Skip to content

Commit

Permalink
react-native link (aka rnpm) support for Windows
Browse files Browse the repository at this point in the history
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
GantMan authored and Facebook Github Bot committed Dec 11, 2016
1 parent f3dbf3e commit 445182c
Show file tree
Hide file tree
Showing 19 changed files with 473 additions and 1 deletion.
3 changes: 3 additions & 0 deletions local-cli/core/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
const android = require('./android');
const findAssets = require('./findAssets');
const ios = require('./ios');
const windows = require('./windows');
const path = require('path');
const wrapCommands = require('./wrapCommands');

Expand All @@ -28,6 +29,7 @@ exports.getProjectConfig = function getProjectConfig() {
return Object.assign({}, rnpm, {
ios: ios.projectConfig(folder, rnpm.ios || {}),
android: android.projectConfig(folder, rnpm.android || {}),
windows: windows.projectConfig(folder, rnpm.windows || {}),
assets: findAssets(folder, rnpm.assets),
});
};
Expand All @@ -46,6 +48,7 @@ exports.getDependencyConfig = function getDependencyConfig(packageName) {
return Object.assign({}, rnpm, {
ios: ios.dependencyConfig(folder, rnpm.ios || {}),
android: android.dependencyConfig(folder, rnpm.android || {}),
windows: windows.dependencyConfig(folder, rnpm.windows || {}),
assets: findAssets(folder, rnpm.assets),
commands: wrapCommands(rnpm.commands),
params: rnpm.params || [],
Expand Down
30 changes: 30 additions & 0 deletions local-cli/core/config/windows/findNamespace.js
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;
};
30 changes: 30 additions & 0 deletions local-cli/core/config/windows/findPackageClassName.js
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;
};
27 changes: 27 additions & 0 deletions local-cli/core/config/windows/findProject.js
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;
};
60 changes: 60 additions & 0 deletions local-cli/core/config/windows/findWindowsSolution.js
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];
};
10 changes: 10 additions & 0 deletions local-cli/core/config/windows/generateGUID.js
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();
}
114 changes: 114 additions & 0 deletions local-cli/core/config/windows/index.js
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,
};
};
32 changes: 31 additions & 1 deletion local-cli/link/link.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ const chalk = require('chalk');
const isEmpty = require('lodash').isEmpty;
const promiseWaterfall = require('./promiseWaterfall');
const registerDependencyAndroid = require('./android/registerNativeModule');
const registerDependencyWindows = require('./windows/registerNativeModule');
const registerDependencyIOS = require('./ios/registerNativeModule');
const isInstalledAndroid = require('./android/isInstalled');
const isInstalledWindows = require('./windows/isInstalled');
const isInstalledIOS = require('./ios/isInstalled');
const copyAssetsAndroid = require('./android/copyAssets');
const copyAssetsIOS = require('./ios/copyAssets');
Expand Down Expand Up @@ -58,6 +60,33 @@ const linkDependencyAndroid = (androidProject, dependency) => {
});
};

const linkDependencyWindows = (windowsProject, dependency) => {

if (!windowsProject || !dependency.config.windows) {
return null;
}

const isInstalled = isInstalledWindows(windowsProject, dependency.config.windows);

if (isInstalled) {
log.info(chalk.grey(`Windows module ${dependency.name} is already linked`));
return null;
}

return pollParams(dependency.config.params).then(params => {
log.info(`Linking ${dependency.name} windows dependency`);

registerDependencyWindows(
dependency.name,
dependency.config.windows,
params,
windowsProject
);

log.info(`Windows module ${dependency.name} has been successfully linked`);
});
};

const linkDependencyIOS = (iOSProject, dependency) => {
if (!iOSProject || !dependency.config.ios) {
return;
Expand Down Expand Up @@ -96,7 +125,7 @@ const linkAssets = (project, assets) => {
};

/**
* Updates project and linkes all dependencies to it
* Updates project and links all dependencies to it
*
* If optional argument [packageName] is provided, it's the only one that's checked
*/
Expand Down Expand Up @@ -128,6 +157,7 @@ function link(args, config) {
() => promisify(dependency.config.commands.prelink || commandStub),
() => linkDependencyAndroid(project.android, dependency),
() => linkDependencyIOS(project.ios, dependency),
() => linkDependencyWindows(project.windows, dependency),
() => promisify(dependency.config.commands.postlink || commandStub),
]));

Expand Down
Loading

0 comments on commit 445182c

Please sign in to comment.