-
Notifications
You must be signed in to change notification settings - Fork 24.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
react-native link (aka rnpm) support for Windows
Summary: Seeing as [Windows is a supported platform](https://github.com/facebook/react-native/blob/72157cf99164d00c7a14b6b9ca51b406080b5265/packager/defaults.js#L22) until platforms can better manager their own CLI and packager needs. Linking 3rd party libraries should be supported first, because then I'd like to do a follow up PR with grabbou to identify how we can effectively move RNPM functionality out of react-native core and eventually housed in each external platform's repo. The goal would be working with cpojer and hopefully andrewimm to help keep external platform needs in their respective repos, for rnpm/packager _et al._ Seeing as this is a major discussion point, I've made this PR first. Making small steps towards this goal, seems to be the approved methodology from all. Additionally, I have a merged PR that makes an excellent place for documenting the CLI when it advances, as preparatio Closes #11282 Differential Revision: D4311391 fbshipit-source-id: be9a836344be4aed6c4732b0ce4947c2a16b6dad
- Loading branch information
Showing
19 changed files
with
473 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const glob = require('glob'); | ||
const path = require('path'); | ||
|
||
/** | ||
* Gets package's namespace | ||
* by searching for its declaration in all C# files present in the folder | ||
* | ||
* @param {String} folder Folder to find C# files | ||
*/ | ||
module.exports = function getNamespace(folder) { | ||
const files = glob.sync('**/*.cs', { cwd: folder }); | ||
|
||
const packages = files | ||
.map(filePath => fs.readFileSync(path.join(folder, filePath), 'utf8')) | ||
.map(file => file.match(/namespace (.*)[\s\S]+IReactPackage/)) | ||
.filter(match => match); | ||
|
||
return packages.length ? packages[0][1] : null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
'use strict'; | ||
|
||
const fs = require('fs'); | ||
const glob = require('glob'); | ||
const path = require('path'); | ||
|
||
/** | ||
* Gets package's class name (class that implements IReactPackage) | ||
* by searching for its declaration in all C# files present in the folder | ||
* | ||
* @param {String} folder Folder to find C# files | ||
*/ | ||
module.exports = function getPackageClassName(folder) { | ||
const files = glob.sync('**/*.cs', { cwd: folder }); | ||
|
||
const packages = files | ||
.map(filePath => fs.readFileSync(path.join(folder, filePath), 'utf8')) | ||
.map(file => file.match(/class (.*) : IReactPackage/)) | ||
.filter(match => match); | ||
|
||
return packages.length ? packages[0][1] : null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
'use strict'; | ||
|
||
const glob = require('glob'); | ||
const path = require('path'); | ||
|
||
/** | ||
* Find an C# project file | ||
* | ||
* @param {String} folder Name of the folder where to seek | ||
* @return {String} | ||
*/ | ||
module.exports = function findManifest(folder) { | ||
const csprojPath = glob.sync(path.join('**', '*.csproj'), { | ||
cwd: folder, | ||
ignore: ['node_modules/**', '**/build/**', 'Examples/**', 'examples/**'], | ||
})[0]; | ||
|
||
return csprojPath ? path.join(folder, csprojPath) : null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
'use strict'; | ||
|
||
const glob = require('glob'); | ||
const path = require('path'); | ||
|
||
/** | ||
* Glob pattern to look for solution file | ||
*/ | ||
const GLOB_PATTERN = '**/*.sln'; | ||
|
||
/** | ||
* Regexp matching all test projects | ||
*/ | ||
const TEST_PROJECTS = /test|example|sample/i; | ||
|
||
/** | ||
* Base windows folder | ||
*/ | ||
const WINDOWS_BASE = 'windows'; | ||
|
||
/** | ||
* These folders will be excluded from search to speed it up | ||
*/ | ||
const GLOB_EXCLUDE_PATTERN = ['**/@(node_modules)/**']; | ||
|
||
/** | ||
* Finds windows project by looking for all .sln files | ||
* in given folder. | ||
* | ||
* Returns first match if files are found or null | ||
* | ||
* Note: `./windows/*.sln` are returned regardless of the name | ||
*/ | ||
module.exports = function findSolution(folder) { | ||
const projects = glob | ||
.sync(GLOB_PATTERN, { | ||
cwd: folder, | ||
ignore: GLOB_EXCLUDE_PATTERN, | ||
}) | ||
.filter(project => { | ||
return path.dirname(project) === WINDOWS_BASE || !TEST_PROJECTS.test(project); | ||
}) | ||
.sort((projectA, projectB) => { | ||
return path.dirname(projectA) === WINDOWS_BASE ? -1 : 1; | ||
}); | ||
|
||
if (projects.length === 0) { | ||
return null; | ||
} | ||
|
||
return projects[0]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const s4 = () => { | ||
return Math.floor((1 + Math.random()) * 0x10000) | ||
.toString(16) | ||
.substring(1); | ||
} | ||
|
||
module.exports = function generateGUID() { | ||
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + | ||
s4() + '-' + s4() + s4() + s4(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
'use strict'; | ||
|
||
const findWindowsSolution = require('./findWindowsSolution'); | ||
const findNamespace = require('./findNamespace'); | ||
const findProject = require('./findProject'); | ||
const findPackageClassName = require('./findPackageClassName'); | ||
const path = require('path'); | ||
const generateGUID = require('./generateGUID'); | ||
|
||
const relativeProjectPath = (fullProjPath) => { | ||
const windowsPath = fullProjPath | ||
.substring(fullProjPath.lastIndexOf("node_modules") - 1, fullProjPath.length) | ||
.replace(/\//g, '\\'); | ||
|
||
return '..' + windowsPath; | ||
} | ||
|
||
const getProjectName = (fullProjPath) => { | ||
return fullProjPath.split('/').slice(-1)[0].replace(/\.csproj/i, ''); | ||
} | ||
|
||
/** | ||
* Gets windows project config by analyzing given folder and taking some | ||
* defaults specified by user into consideration | ||
*/ | ||
exports.projectConfig = function projectConfigWindows(folder, userConfig) { | ||
|
||
const csSolution = userConfig.csSolution || findWindowsSolution(folder); | ||
const solutionPath = path.join(folder, csSolution); | ||
|
||
if (!csSolution) { | ||
return null; | ||
} | ||
|
||
// expects solutions to be named the same as project folders | ||
const windowsAppFolder = csSolution.substring(0, csSolution.lastIndexOf(".sln")); | ||
const src = userConfig.sourceDir || windowsAppFolder; | ||
const sourceDir = path.join(folder, src); | ||
const mainPage = path.join(sourceDir, 'MainPage.cs'); | ||
const projectPath = userConfig.projectPath || findProject(folder); | ||
|
||
return { | ||
sourceDir, | ||
solutionPath, | ||
projectPath, | ||
mainPage, | ||
folder, | ||
userConfig, | ||
}; | ||
}; | ||
|
||
/** | ||
* Same as projectConfigWindows except it returns | ||
* different config that applies to packages only | ||
*/ | ||
exports.dependencyConfig = function dependencyConfigWindows(folder, userConfig) { | ||
|
||
const csSolution = userConfig.csSolution || findWindowsSolution(folder); | ||
|
||
if (!csSolution) { | ||
return null; | ||
} | ||
|
||
// expects solutions to be named the same as project folders | ||
const windowsAppFolder = csSolution.substring(0, csSolution.lastIndexOf(".sln")); | ||
const src = userConfig.sourceDir || windowsAppFolder; | ||
|
||
if (!src) { | ||
return null; | ||
} | ||
|
||
const sourceDir = path.join(folder, src); | ||
const packageClassName = findPackageClassName(sourceDir); | ||
const namespace = userConfig.namespace || findNamespace(sourceDir); | ||
const csProj = userConfig.csProj || findProject(folder); | ||
|
||
/** | ||
* This module has no package to export or no namespace | ||
*/ | ||
if (!packageClassName || !namespace) { | ||
return null; | ||
} | ||
|
||
const packageUsingPath = userConfig.packageUsingPath || | ||
`using ${namespace};`; | ||
|
||
const packageInstance = userConfig.packageInstance || | ||
`new ${packageClassName}()`; | ||
|
||
const projectGUID = generateGUID(); | ||
const pathGUID = generateGUID(); | ||
const projectName = getProjectName(csProj); | ||
const relativeProjPath = relativeProjectPath(csProj); | ||
|
||
return { | ||
sourceDir, | ||
packageUsingPath, | ||
packageInstance, | ||
projectName, | ||
csProj, | ||
folder, | ||
projectGUID, | ||
pathGUID, | ||
relativeProjPath, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.