Skip to content

Commit

Permalink
fix: build
Browse files Browse the repository at this point in the history
  • Loading branch information
kokororin committed Sep 1, 2023
1 parent c7554bd commit 008e676
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 56 deletions.
29 changes: 19 additions & 10 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
name: Deploy

on:
push:
branches:
- master
schedule:
- cron: "0 * * * *"

jobs:
deploy:
Expand All @@ -27,6 +26,7 @@ jobs:
uses: actions/setup-node@v3
with:
node-version: '18.x'
registry-url: 'https://registry.npmjs.org'

- name: Cache node modules
uses: actions/cache@v3
Expand All @@ -46,14 +46,23 @@ jobs:
yarn global add @vscode/vsce ovsx
yarn install
- name: Run deploy script
run: |
npm run build
npx tsx ./scripts/deploy.ts
npm publish --ignore-scripts
vsce publish -p ${{ env.VSCE_TOKEN }}
ovsx publish -p ${{ env.OVSX_TOKEN }}
- name: Build
run: npm run build

- name: Run Deploy
run: npx tsx ./scripts/deploy.ts

- name: Publish to NPM
run: npm publish --ignore-scripts || true
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

- name: Publish to VSCE
run: vsce publish -p ${{ env.VSCE_TOKEN }} || true
env:
VSCE_TOKEN: ${{ secrets.VSCE_TOKEN }}

- name: Publish to OVSX
run: ovsx publish -p ${{ env.OVSX_TOKEN }} || true
env:
OVSX_TOKEN: ${{ secrets.OVSX_TOKEN }}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@
"detect-indent": "^6.0.0",
"find-up": "^5.0.0",
"mem": "^8.1.1",
"phpfmt": "^0.0.5",
"phpfmt": "^0.0.6",
"pjson": "^1.0.9",
"semver": "^7.5.0"
}
Expand Down
31 changes: 12 additions & 19 deletions scripts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,13 @@ import { downloadFile } from '../src/utils';

const pkgJsonPath = path.join(__dirname, '../package.json');
const changelogPath = path.join(__dirname, '../CHANGELOG.md');
const currentPharPath = path.join(
__dirname,
'../node_modules/phpfmt/v2/fmt.stub.php'
);

void (async () => {
try {
const pkg = JSON.parse(String(await fs.promises.readFile(pkgJsonPath)));
const pharFilePath = phpfmt.v2.pharPath;
const currentVersion = pkg.version;

const baseDir = path.resolve(pharFilePath, '..');
const installFile = path.join(baseDir, 'install.js');
const installContent = String(await fs.promises.readFile(installFile));
const regex = /(var|const|let)\s+url\s+=\s+'(https?:\/\/[^']+)'/s;
const match = installContent.match(regex);
if (!match?.[2]) {
throw new Error('cannot find download url');
}
const pharUrl = match[2];
const pharUrl = phpfmt.v2.installUrl;
console.log(`Download url: ${pharUrl}`);

const tmpDir = path.join(os.tmpdir(), 'vscode-phpfmt');
Expand All @@ -38,18 +25,25 @@ void (async () => {
await fs.promises.mkdir(tmpDir);
}
const currentVsixPath = path.join(tmpDir, `${currentVersion}.vsix`);
const latestPharPath = path.join(tmpDir, 'fmt.stub.php');
const latestPharPath = path.join(tmpDir, phpfmt.v2.pharName);

console.log('Downloading vsix...');
await downloadFile(
`https://kokororin.gallery.vsassets.io/_apis/public/gallery/publisher/kokororin/extension/vscode-phpfmt/${currentVersion}/assetbyname/Microsoft.VisualStudio.Services.VSIXPackage`,
currentVsixPath
);

const stats = await fs.promises.stat(currentVsixPath);
if (stats.size < 10000) {
console.log('Download vsix failed');
return;
}

const zip = new AdmZip(currentVsixPath);
const zipEntries = zip.getEntries();
const entry = zipEntries.find(
o => o.entryName === 'extension/node_modules/phpfmt/v2/fmt.stub.php'
o =>
o.entryName === `extension/node_modules/phpfmt/v2/${phpfmt.v2.pharName}`
);
const currentPharData = String(entry?.getData());
const currentMd5 = md5(currentPharData);
Expand All @@ -72,7 +66,7 @@ void (async () => {
let changelogData = String(await fs.promises.readFile(changelogPath));
changelogData = `### ${newVersion}
- Upgrade fmt.stub.php (${latestMd5})
- Upgrade ${phpfmt.v2.pharName} (${latestMd5})
${changelogData}`;
await fs.promises.writeFile(changelogPath, changelogData);
Expand All @@ -83,7 +77,7 @@ ${changelogData}`;
JSON.stringify(pkg, null, 2) + os.EOL
);

await fs.promises.writeFile(currentPharPath, latestPharData);
await fs.promises.writeFile(phpfmt.v2.pharPath, latestPharData);

const git = simpleGit({
config: [
Expand All @@ -101,7 +95,6 @@ ${changelogData}`;
.addTag(`v${newVersion}`)
.push()
.pushTags();

} catch (err) {
console.error(err);
process.exit(1);
Expand Down
34 changes: 12 additions & 22 deletions src/PHPFmtProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
type QuickPickItem,
type WorkspaceConfiguration
} from 'vscode';
import path from 'path';
import fs from 'fs';
import pkg from 'pjson';
import type { PHPFmt } from './PHPFmt';
Expand Down Expand Up @@ -62,29 +61,20 @@ export class PHPFmtProvider {
const destFile = this.phpfmt.getFmt().pharPath;
const bakFile = `${this.phpfmt.getFmt().pharPath}.bak`;

const baseDir = path.resolve(destFile, '..');
const installFile = path.join(baseDir, 'install.js');
const installContent = String(await fs.promises.readFile(installFile));
const regex = /(var|const|let)\s+url\s+=\s+'(https?:\/\/[^']+)'/s;
const match = installContent.match(regex);
if (match?.[2]) {
const url = match[2];
this.widget.logInfo(`Download url: ${url}`);
const url = this.phpfmt.getFmt().installUrl;
this.widget.logInfo(`Download url: ${url}`);

await fs.promises.copyFile(destFile, bakFile);
await fs.promises.copyFile(destFile, bakFile);

try {
await downloadFile(url, destFile);
this.widget.logInfo(`Download fmt to "${destFile}" successfully`);
await Window.showInformationMessage(
'fmt.phar or fmt.stub.php upgraded successfully!'
);
} catch (err) {
this.widget.logError('Download failed', err);
await fs.promises.copyFile(bakFile, destFile);
}
} else {
throw new Error('Failed to get url in modules');
try {
await downloadFile(url, destFile);
this.widget.logInfo(`Download fmt to "${destFile}" successfully`);
await Window.showInformationMessage(
'fmt.phar or fmt.stub.php upgraded successfully!'
);
} catch (err) {
this.widget.logError('Download failed', err);
await fs.promises.copyFile(bakFile, destFile);
}
} catch (err) {
await Window.showErrorMessage(
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2386,10 +2386,10 @@ php-parser@^3.1.4:
resolved "https://registry.yarnpkg.com/php-parser/-/php-parser-3.1.4.tgz#165a594bf9d7fb516b89d1ecf62a361083a482f8"
integrity sha512-WUEfH4FWsVItqgOknM67msDdcUAfgPJsHhPNl6EPXzWtX+PfdY282m4i8YIJ9ALUEhf+qGDajdmW+VYqSd7Deg==

phpfmt@^0.0.5:
version "0.0.5"
resolved "https://registry.yarnpkg.com/phpfmt/-/phpfmt-0.0.5.tgz#bbbf5b8534308735453fd2a46606677fcd955e65"
integrity sha512-2ube9QII6r0nPTNIbFFAx0Zxt/kZKQws+xGdG2j4hZNbJtQ3zjOMFh9mcSMUYwTbNkHMJ+C+/8+cA2LFGpJ6nw==
phpfmt@^0.0.6:
version "0.0.6"
resolved "https://registry.yarnpkg.com/phpfmt/-/phpfmt-0.0.6.tgz#03ed77860e6ccf5037d851eec98e6aa012608ab3"
integrity sha512-Pw+4/73cNsi2ai1Oq3Qvvw7AagPYEetWCRvbiVgfbIJJFUajJocsblUpsUC6u0arFIrdZ951aHP0AyyVXl/avQ==
dependencies:
php-parser "^3.1.4"

Expand Down

0 comments on commit 008e676

Please sign in to comment.