Skip to content

Commit

Permalink
Filter data from other platforms (#912)
Browse files Browse the repository at this point in the history
* Filter data from other platforms
When the parameter target has a value, all files will be filtered based on the target value. All file directories are in the target, but directories with different values from the target will be excluded from this packaging.
For example:
ui
 |-style
     |- linux-x64
     |      |- default.css
     |- win32-x64
            |- default.css

When using vsce package - t win32-x64 for packaging, the linux-x64 directory will not be packaged into the program。

当参数 target 有值时,会根据target值过滤所有文件,所有文件目录在Target中,但时和target值不相同的目录,将会再本次打包中排除 。
如果:
ui
 |-style
     |- linux-x64
     |      |- default.css
     |- win32-x64
            |- default.css

在使用vsce package -t win32-x64 进行打包时,linux-x64目录将不会打包到程序里

* Add target and ignoreOtherTargetFolders options

---------

Co-authored-by: 曾洪亮 <[email protected]>
Co-authored-by: João Moreno <[email protected]>
  • Loading branch information
3 people authored Dec 15, 2023
1 parent 3e8410b commit 160d0e0
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 24 deletions.
6 changes: 6 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ module.exports = function (argv: string[]): void {
.description('Packages an extension')
.option('-o, --out <path>', 'Output .vsix extension file to <path> location (defaults to <name>-<version>.vsix)')
.option('-t, --target <target>', `Target architecture. Valid targets: ${ValidTargets}`)
.option('--ignore-other-target-folders', `Ignore other target folders. Valid only when --target <target> is provided.`)
.option('-m, --message <commit message>', 'Commit message used when calling `npm version`.')
.option(
'--no-git-tag-version',
Expand Down Expand Up @@ -120,6 +121,7 @@ module.exports = function (argv: string[]): void {
{
out,
target,
ignoreOtherTargetFolders,
message,
gitTagVersion,
updatePackageJson,
Expand All @@ -144,6 +146,7 @@ module.exports = function (argv: string[]): void {
packagePath: out,
version,
target,
ignoreOtherTargetFolders,
commitMessage: message,
gitTagVersion,
updatePackageJson,
Expand Down Expand Up @@ -174,6 +177,7 @@ module.exports = function (argv: string[]): void {
process.env['VSCE_PAT']
)
.option('-t, --target <targets...>', `Target architectures. Valid targets: ${ValidTargets}`)
.option('--ignore-other-target-folders', `Ignore other target folders. Valid only when --target <target> is provided.`)
.option('-m, --message <commit message>', 'Commit message used when calling `npm version`.')
.option(
'--no-git-tag-version',
Expand Down Expand Up @@ -211,6 +215,7 @@ module.exports = function (argv: string[]): void {
{
pat,
target,
ignoreOtherTargetFolders,
message,
gitTagVersion,
updatePackageJson,
Expand All @@ -237,6 +242,7 @@ module.exports = function (argv: string[]): void {
pat,
version,
targets: target,
ignoreOtherTargetFolders,
commitMessage: message,
gitTagVersion,
updatePackageJson,
Expand Down
66 changes: 42 additions & 24 deletions src/package.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,17 @@ export interface IPackageOptions {
* https://code.visualstudio.com/api/working-with-extensions/publishing-extension#platformspecific-extensions
*/
readonly target?: string;

/**
* Ignore all files inside folders named as other targets. Only relevant when
* `target` is set. For example, if `target` is `linux-x64` and there are
* folders named `win32-x64`, `darwin-arm64` or `web`, the files inside
* those folders will be ignored.
*
* @default false
*/
readonly ignoreOtherTargetFolders?: boolean;

readonly commitMessage?: string;
readonly gitTagVersion?: boolean;
readonly updatePackageJson?: boolean;
Expand Down Expand Up @@ -1538,7 +1549,7 @@ async function collectAllFiles(
const promises = deps.map(dep =>
promisify(glob)('**', { cwd: dep.src, nodir: true, dot: true, ignore: 'node_modules/**' }).then(files =>
files.map(f => ({
src: path.relative(cwd, path.join(dep.src, f.replace(/\\/g, '/'))),
src: path.relative(cwd, path.join(dep.src, f)).replace(/\\/g, '/'),
dest: path.join(dep.dest, f).replace(/\\/g, '/')
}))
)
Expand All @@ -1547,16 +1558,39 @@ async function collectAllFiles(
return Promise.all(promises).then(util.flatten);
}

function getDependenciesOption(options: IPackageOptions): 'npm' | 'yarn' | 'none' | undefined {
if (options.dependencies === false) {
return 'none';
}

switch (options.useYarn) {
case true:
return 'yarn';
case false:
return 'npm';
default:
return undefined;
}
}

function collectFiles(
cwd: string,
manifest: Manifest,
dependencies: 'npm' | 'yarn' | 'none' | undefined,
dependencyEntryPoints?: string[],
ignoreFile?: string
options: IPackageOptions
): Promise<SourceAndDestination[]> {
return collectAllFiles(cwd, manifest, dependencies, dependencyEntryPoints).then(files => {
const packagedDependencies = options.dependencyEntryPoints || undefined;
const ignoreFile = options.ignoreFile || undefined;
const target = options.target || undefined;

return collectAllFiles(cwd, manifest, getDependenciesOption(options), packagedDependencies).then(files => {
files = files.filter(f => !/\r$/m.test(f.src));

// Filter data from other platforms
if (target && options.ignoreOtherTargetFolders) {
const regex = new RegExp(`(^|/)(${Array.from(Targets, v => v).filter(v => v !== target).join('|')})/`);
files = files.filter(f => !regex.test(f.src))
}

return (
fs.promises
.readFile(ignoreFile ? ignoreFile : path.join(cwd, '.vscodeignore'), 'utf8')
Expand Down Expand Up @@ -1646,28 +1680,11 @@ export function createDefaultProcessors(manifest: Manifest, options: IPackageOpt
];
}

function getDependenciesOption(options: IListFilesOptions): 'npm' | 'yarn' | 'none' | undefined {
if (options.dependencies === false) {
return 'none';
}

switch (options.useYarn) {
case true:
return 'yarn';
case false:
return 'npm';
default:
return undefined;
}
}

export function collect(manifest: Manifest, options: IPackageOptions = {}): Promise<IFile[]> {
const cwd = options.cwd || process.cwd();
const packagedDependencies = options.dependencyEntryPoints || undefined;
const ignoreFile = options.ignoreFile || undefined;
const processors = createDefaultProcessors(manifest, options);

return collectFiles(cwd, manifest, getDependenciesOption(options), packagedDependencies, ignoreFile).then(fileNames => {
return collectFiles(cwd, manifest, options).then(fileNames => {
const files = fileNames.map(f => ({ path: `extension/${f.dest}`, localPath: path.join(cwd, f.src) }));

return processFiles(processors, files);
Expand Down Expand Up @@ -1819,7 +1836,8 @@ export async function listFiles(options: IListFilesOptions = {}): Promise<string
await prepublish(cwd, manifest, options.useYarn);
}

return (await collectFiles(cwd, manifest, getDependenciesOption(options), options.packagedDependencies, options.ignoreFile)).map(f => f.src);
const files = await collectFiles(cwd, manifest, options);
return files.map(f => f.src);
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface IPublishOptions {
readonly packagePath?: string[];
readonly version?: string;
readonly targets?: string[];
readonly ignoreOtherTargetFolders?: boolean;
readonly commitMessage?: string;
readonly gitTagVersion?: boolean;
readonly updatePackageJson?: boolean;
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
6 changes: 6 additions & 0 deletions src/test/fixtures/target/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "target",
"publisher": "joaomoreno",
"version": "1.0.0",
"engines": { "vscode": "*" }
}
Empty file.
Empty file.
42 changes: 42 additions & 0 deletions src/test/package.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,48 @@ describe('collect', function () {
const ignore = files.find(f => f.path === ignoreFilename);
assert.ok(!ignore, 'should ignore ' + ignoreFilename)
});

it('should handle target and ignoreOtherTargetFolders', async function () {
const cwd = fixture('target');
const manifest = await readManifest(cwd);
let files = await collect(manifest, { cwd });

assert.strictEqual(files.length, 13);
assert.ok(files.some(f => f.path === 'extension/file.txt'));
assert.ok(files.some(f => f.path === 'extension/random/file.txt'));
assert.ok(files.some(f => f.path === 'extension/deep/file.txt'));
assert.ok(files.some(f => f.path === 'extension/deep/random/file.txt'));
assert.ok(files.some(f => f.path === 'extension/linux-x64/file.txt'));
assert.ok(files.some(f => f.path === 'extension/deep/linux-x64/file.txt'));
assert.ok(files.some(f => f.path === 'extension/web/file.txt'));
assert.ok(files.some(f => f.path === 'extension/deep/web/file.txt'));
assert.ok(files.some(f => f.path === 'extension/darwin-arm64/file.txt'));
assert.ok(files.some(f => f.path === 'extension/deep/darwin-arm64/file.txt'));

files = await collect(manifest, { cwd, target: 'linux-x64' });

assert.strictEqual(files.length, 13);
assert.ok(files.some(f => f.path === 'extension/file.txt'));
assert.ok(files.some(f => f.path === 'extension/random/file.txt'));
assert.ok(files.some(f => f.path === 'extension/deep/file.txt'));
assert.ok(files.some(f => f.path === 'extension/deep/random/file.txt'));
assert.ok(files.some(f => f.path === 'extension/linux-x64/file.txt'));
assert.ok(files.some(f => f.path === 'extension/deep/linux-x64/file.txt'));
assert.ok(files.some(f => f.path === 'extension/web/file.txt'));
assert.ok(files.some(f => f.path === 'extension/deep/web/file.txt'));
assert.ok(files.some(f => f.path === 'extension/darwin-arm64/file.txt'));
assert.ok(files.some(f => f.path === 'extension/deep/darwin-arm64/file.txt'));

files = await collect(manifest, { cwd, target: 'linux-x64', ignoreOtherTargetFolders: true });

assert.strictEqual(files.length, 9);
assert.ok(files.some(f => f.path === 'extension/file.txt'));
assert.ok(files.some(f => f.path === 'extension/random/file.txt'));
assert.ok(files.some(f => f.path === 'extension/deep/file.txt'));
assert.ok(files.some(f => f.path === 'extension/deep/random/file.txt'));
assert.ok(files.some(f => f.path === 'extension/linux-x64/file.txt'));
assert.ok(files.some(f => f.path === 'extension/deep/linux-x64/file.txt'));
});
});

describe('readManifest', () => {
Expand Down

0 comments on commit 160d0e0

Please sign in to comment.