Skip to content

Commit

Permalink
feat: cli --version (#54)
Browse files Browse the repository at this point in the history

Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
  • Loading branch information
jkowalleck authored Mar 29, 2024
1 parent 735e8d3 commit a78db7c
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 11 deletions.
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/ValidationError-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ Steps to reproduce the behavior:

## Environment

- _@cyclonedx/yarn-plugin-cyclonedx_ version: <!-- e.g. `1.0.0-alpha+4c1de92` -->
- yarn version: <!-- get via `yarn -v` -->
- _@cyclonedx/yarn-plugin-cyclonedx_ version: <!-- e.g. `v1.0.0-alpha+4c1de92`, get via `yarn cyclonedx --version` -->
- yarn version: <!-- get via `yarn --version` -->
- Node version: <!-- get via `node --version` -->
- OS: <!-- e.g. windows 11, ubuntu linux, ... -->

Expand Down
4 changes: 2 additions & 2 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ If applicable, add screenshots or past the output to help explain your problem.

## Environment

- _@cyclonedx/yarn-plugin-cyclonedx_ version: <!-- e.g. `1.0.0-alpha+4c1de92` -->
- yarn version: <!-- get via `yarn -v` -->
- _@cyclonedx/yarn-plugin-cyclonedx_ version: <!-- e.g. `v1.0.0-alpha+4c1de92`, get via `yarn cyclonedx --version` -->
- yarn version: <!-- get via `yarn --version` -->
- Node version: <!-- get via `node --version` -->
- OS: <!-- e.g. windows 11, ubuntu linux, ... -->

Expand Down
11 changes: 10 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ class CycloneCommand extends BaseCommand {
}
}

export class CycloneVersionCommand extends Command<any> {
static override readonly paths = CycloneCommand.paths.map(p => [...p, '--version'])

async execute (): Promise<void> {
const { self } = await import('./buildtimeInfo.json')
this.context.stdout.write(`${self.name} v${self.version}\n`)
}
}

export default {
commands: [CycloneCommand]
commands: [CycloneCommand, CycloneVersionCommand]
} satisfies Plugin
33 changes: 28 additions & 5 deletions tests/integration/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const path = require('path')
const { existsSync, writeFileSync, readFileSync } = require('fs')
const { constants: { MAX_LENGTH: BUFFER_MAX_LENGTH } } = require('buffer')

const { version: thisVersion } = require('../../package.json')
const { name: thisName, version: thisVersion } = require('../../package.json')

const testSetups = [
/* region functional tests */
Expand Down Expand Up @@ -111,6 +111,7 @@ suite('integration', () => {
}).timeout(longTestTimeout)
})
})

suite('prod', () => {
testSetups.filter(c => c.startsWith('dev-')).forEach((testSetup) => {
test(`arg: ${testSetup}`, () => {
Expand All @@ -121,6 +122,24 @@ suite('integration', () => {
})
})
})

test('version', () => {
const res = spawnSync(
'yarn', ['cyclonedx', '--version'], {
cwd: projectRootPath,
stdio: ['ignore', 'pipe', 'pipe'],
encoding: 'utf8',
shell: true,
env: {
PATH: process.env.PATH,
CI: '1',
YARN_PLUGINS: thisYarnPlugin
}
})
assert.strictEqual(res.error, undefined)
assert.strictEqual(res.status, 0, res.output)
assert.ok(res.stdout.startsWith(`${thisName} v${thisVersion}`), res.stdout)
})
})
})

Expand Down Expand Up @@ -149,9 +168,11 @@ function makeJsonReproducible (json) {
return json
.replace(
// replace metadata.tools.version
' "vendor": "@cyclonedx",\n' +
new RegExp(
' "vendor": "@cyclonedx",\n' +
' "name": "yarn-plugin-cyclonedx",\n' +
` "version": ${JSON.stringify(thisVersion)},\n`,
` "version": "${JSON.stringify(thisVersion).slice(1, -1)}(?:\\+.+)?",\n`
),
' "vendor": "@cyclonedx",\n' +
' "name": "yarn-plugin-cyclonedx",\n' +
' "version": "thisVersion-testing",\n'
Expand All @@ -178,9 +199,11 @@ function makeXmlReproducible (xml) {
return xml
.replace(
// replace metadata.tools.version
' <vendor>@cyclonedx</vendor>\n' +
new RegExp(
' <vendor>@cyclonedx</vendor>\n' +
' <name>yarn-plugin-cyclonedx</name>\n' +
` <version>${thisVersion}</version>`,
` <version>${thisVersion}(?:\\+.+)?</version>`
),
' <vendor>@cyclonedx</vendor>\n' +
' <name>yarn-plugin-cyclonedx</name>\n' +
' <version>thisVersion-testing</version>'
Expand Down
13 changes: 12 additions & 1 deletion tools/gather-buildtime-info.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,21 @@ const selfNfo = JSON.parse(fs.readFileSync(
path.join(projectRootPath, 'package.json'),
'utf8'))

let buildMeta = ''
try {
buildMeta = '+git.' + execFileSync('git', ['rev-parse', 'HEAD'], {
cwd: projectRootPath,
encoding: 'utf8',
stdio: ['ignore', 'pipe', 'ignore']
}).trim().substring(0, 7)
} catch (err) {
console.debug('failed fetching guild meta ...', err)
}

const data = {
self: {
name: selfNfo.name,
version: selfNfo.version,
version: selfNfo.version + buildMeta,
homepage: selfNfo.homepage,
repository: selfNfo.repository,
bugs: selfNfo.bugs
Expand Down

0 comments on commit a78db7c

Please sign in to comment.