feat(action) add Cargo.toml formatting/parsing

This commit is contained in:
Lucas Nogueira
2020-07-09 00:09:06 -03:00
parent b6a3e08e23
commit 35b844226e
6 changed files with 76 additions and 19 deletions

View File

@@ -42,6 +42,6 @@ jobs:
projectPath: ./example
tagName: example-v__VERSION__
releaseName: "Release example app v__VERSION__"
body: "See the assets to download this version and install."
draft: true
releaseBody: "See the assets to download this version and install."
releaseDraft: true
prerelease: false

View File

@@ -51,8 +51,8 @@ jobs:
with:
tagName: app-v__VERSION__
releaseName: "App v__VERSION__"
body: "See the assets to download this version and install."
draft: true
releaseBody: "See the assets to download this version and install."
releaseDraft: true
prerelease: false
```

34
dist/main.js vendored
View File

@@ -38,6 +38,7 @@ const path_1 = require("path");
const fs_1 = require("fs");
const upload_release_assets_1 = __importDefault(require("./upload-release-assets"));
const create_release_1 = __importDefault(require("./create-release"));
const toml_1 = __importDefault(require("@iarna/toml"));
function getPackageJson(root) {
const packageJsonPath = path_1.join(root, 'package.json');
if (fs_1.existsSync(packageJsonPath)) {
@@ -77,14 +78,33 @@ function buildProject(root, debug, { configPath, distPath }) {
}
})
.then((runner) => {
if (fs_1.existsSync(path_1.join(root, 'src-tauri'))) {
return runner;
const manifestPath = path_1.join(root, 'src-tauri/Cargo.toml');
if (fs_1.existsSync(manifestPath)) {
const cargoManifest = toml_1.default.parse(fs_1.readFileSync(manifestPath).toString());
return {
runner,
name: cargoManifest.package.name,
version: cargoManifest.package.version
};
}
else {
return execCommand(`${runner} init`, { cwd: root }).then(() => runner);
return execCommand(`${runner} init`, { cwd: root }).then(() => {
const cargoManifest = toml_1.default.parse(fs_1.readFileSync(manifestPath).toString());
const packageJson = getPackageJson(root);
const appName = packageJson ? (packageJson.displayName || packageJson.name) : 'app';
const version = packageJson ? packageJson.version : '0.1.0';
cargoManifest.package.name = appName;
cargoManifest.package.version = version;
fs_1.writeFileSync(manifestPath, toml_1.default.stringify(cargoManifest));
return {
runner,
name: appName,
version
};
});
}
})
.then((runner) => {
.then((app) => {
const tauriConfPath = path_1.join(root, 'src-tauri/tauri.conf.json');
if (configPath !== null) {
fs_1.copyFileSync(configPath, tauriConfPath);
@@ -95,8 +115,8 @@ function buildProject(root, debug, { configPath, distPath }) {
fs_1.writeFileSync(tauriConfPath, JSON.stringify(tauriConf));
}
const args = debug ? ['--debug'] : [];
return execCommand(`${runner} build` + (args.length ? ` ${args.join(' ')}` : ''), { cwd: root }).then(() => {
const appName = 'app'; // TODO read from Cargo.toml
return execCommand(`${app.runner} build` + (args.length ? ` ${args.join(' ')}` : ''), { cwd: root }).then(() => {
const appName = app.name;
const artifactsPath = path_1.join(root, `src-tauri/target/${debug ? 'debug' : 'release'}`);
switch (os_1.platform()) {
case 'darwin':
@@ -110,7 +130,7 @@ function buildProject(root, debug, { configPath, distPath }) {
];
default:
return [
path_1.join(artifactsPath, `bundle/deb/${appName}.deb`),
path_1.join(artifactsPath, `bundle/deb/${appName}_${app.version}_amd64.deb`),
path_1.join(artifactsPath, `bundle/appimage/${appName}.AppImage`)
];
}

View File

@@ -26,6 +26,7 @@
"dependencies": {
"@actions/core": "^1.2.0",
"@actions/github": "^2.2.0",
"@iarna/toml": "^2.2.5",
"execa": "^4.0.3"
},
"devDependencies": {
@@ -43,4 +44,4 @@
"ts-jest": "^24.2.0",
"typescript": "^3.6.4"
}
}
}

View File

@@ -5,6 +5,7 @@ import { join, resolve } from 'path'
import { readFileSync, existsSync, copyFileSync, writeFileSync } from 'fs'
import uploadReleaseAssets from './upload-release-assets'
import createRelease from './create-release'
import toml from '@iarna/toml'
function getPackageJson(root: string): any {
const packageJsonPath = join(root, 'package.json')
@@ -37,6 +38,16 @@ function execCommand(command: string, { cwd }: { cwd: string | undefined }): Pro
}).then()
}
interface CargoManifest {
package: { version: string, name: string }
}
interface Application {
runner: string
name: string
version: string
}
async function buildProject(root: string, debug: boolean, { configPath, distPath }: { configPath: string | null, distPath: string | null }): Promise<string[]> {
return new Promise<string>((resolve) => {
if (hasTauriDependency(root)) {
@@ -47,13 +58,33 @@ async function buildProject(root: string, debug: boolean, { configPath, distPath
}
})
.then((runner: string) => {
if (existsSync(join(root, 'src-tauri'))) {
return runner
const manifestPath = join(root, 'src-tauri/Cargo.toml')
if (existsSync(manifestPath)) {
const cargoManifest = toml.parse(readFileSync(manifestPath).toString()) as any as CargoManifest
return {
runner,
name: cargoManifest.package.name,
version: cargoManifest.package.version
}
} else {
return execCommand(`${runner} init`, { cwd: root }).then(() => runner)
return execCommand(`${runner} init`, { cwd: root }).then(() => {
const cargoManifest = toml.parse(readFileSync(manifestPath).toString()) as any as CargoManifest
const packageJson = getPackageJson(root)
const appName = packageJson ? (packageJson.displayName || packageJson.name) : 'app'
const version = packageJson ? packageJson.version : '0.1.0'
cargoManifest.package.name = appName
cargoManifest.package.version = version
writeFileSync(manifestPath, toml.stringify(cargoManifest as any))
return {
runner,
name: appName,
version
}
})
}
})
.then((runner: string) => {
.then((app: Application) => {
const tauriConfPath = join(root, 'src-tauri/tauri.conf.json')
if (configPath !== null) {
copyFileSync(configPath, tauriConfPath)
@@ -66,8 +97,8 @@ async function buildProject(root: string, debug: boolean, { configPath, distPath
}
const args = debug ? ['--debug'] : []
return execCommand(`${runner} build` + (args.length ? ` ${args.join(' ')}` : ''), { cwd: root }).then(() => {
const appName = 'app' // TODO read from Cargo.toml
return execCommand(`${app.runner} build` + (args.length ? ` ${args.join(' ')}` : ''), { cwd: root }).then(() => {
const appName = app.name
const artifactsPath = join(root, `src-tauri/target/${debug ? 'debug' : 'release'}`)
switch (platform()) {
@@ -82,7 +113,7 @@ async function buildProject(root: string, debug: boolean, { configPath, distPath
]
default:
return [
join(artifactsPath, `bundle/deb/${appName}.deb`),
join(artifactsPath, `bundle/deb/${appName}_${app.version}_amd64.deb`),
join(artifactsPath, `bundle/appimage/${appName}.AppImage`)
]
}

View File

@@ -341,6 +341,11 @@
aggregate-error "3.0.1"
tslib "~2.0.0"
"@iarna/toml@^2.2.5":
version "2.2.5"
resolved "https://registry.yarnpkg.com/@iarna/toml/-/toml-2.2.5.tgz#b32366c89b43c6f8cefbdefac778b9c828e3ba8c"
integrity sha512-trnsAYxU3xnS1gPHPyU961coFyLkh4gAD/0zQ5mymY4yOZ+CYvsPqUbOFSw0aDM4y0tV7tiFxL/1XfXPNC6IPg==
"@jest/console@^24.7.1", "@jest/console@^24.9.0":
version "24.9.0"
resolved "https://registry.yarnpkg.com/@jest/console/-/console-24.9.0.tgz#79b1bc06fb74a8cfb01cbdedf945584b1b9707f0"