mirror of
				https://github.com/crazy-max/ghaction-upx.git
				synced 2025-11-03 20:48:11 -07:00 
			
		
		
		
	Use native tools
This commit is contained in:
		
							parent
							
								
									232b78f307
								
							
						
					
					
						commit
						6395a3aa2f
					
				
							
								
								
									
										9
									
								
								__tests__/github.test.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								__tests__/github.test.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,9 @@
 | 
				
			|||||||
 | 
					import * as github from '../src/github';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					describe('github', () => {
 | 
				
			||||||
 | 
					  it('returns 3.96 GitHub release', async () => {
 | 
				
			||||||
 | 
					    const release = await github.getRelease('3.96');
 | 
				
			||||||
 | 
					    expect(release).not.toBeNull();
 | 
				
			||||||
 | 
					    expect(release.tag_name).toEqual('v3.96');
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
							
								
								
									
										837
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
							
						
						
									
										837
									
								
								package-lock.json
									
									
									
										generated
									
									
									
								
							
										
											
												File diff suppressed because it is too large
												Load Diff
											
										
									
								
							@ -22,14 +22,12 @@
 | 
				
			|||||||
  "dependencies": {
 | 
					  "dependencies": {
 | 
				
			||||||
    "@actions/core": "^1.2.4",
 | 
					    "@actions/core": "^1.2.4",
 | 
				
			||||||
    "@actions/exec": "^1.0.4",
 | 
					    "@actions/exec": "^1.0.4",
 | 
				
			||||||
    "@actions/tool-cache": "^1.3.4",
 | 
					    "@actions/http-client": "^1.0.8",
 | 
				
			||||||
    "download": "^8.0.0",
 | 
					    "@actions/tool-cache": "^1.3.4"
 | 
				
			||||||
    "typed-rest-client": "^1.7.3"
 | 
					 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
  "devDependencies": {
 | 
					  "devDependencies": {
 | 
				
			||||||
    "@types/jest": "^25.2.1",
 | 
					    "@types/jest": "^25.2.1",
 | 
				
			||||||
    "@types/node": "^13.13.4",
 | 
					    "@types/node": "^13.13.4",
 | 
				
			||||||
    "@types/download": "^6.2.4",
 | 
					 | 
				
			||||||
    "@zeit/ncc": "^0.22.1",
 | 
					    "@zeit/ncc": "^0.22.1",
 | 
				
			||||||
    "jest": "^25.5.0",
 | 
					    "jest": "^25.5.0",
 | 
				
			||||||
    "jest-circus": "^25.5.0",
 | 
					    "jest-circus": "^25.5.0",
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										12
									
								
								src/github.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								src/github.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,12 @@
 | 
				
			|||||||
 | 
					import * as httpm from '@actions/http-client';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export interface GitHubRelease {
 | 
				
			||||||
 | 
					  id: number;
 | 
				
			||||||
 | 
					  tag_name: string;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const getRelease = async (version: string): Promise<GitHubRelease | null> => {
 | 
				
			||||||
 | 
					  const url: string = `https://github.com/upx/upx/releases/${version !== 'latest' ? `v${version}` : version}`;
 | 
				
			||||||
 | 
					  const http: httpm.HttpClient = new httpm.HttpClient('ghaction-upx');
 | 
				
			||||||
 | 
					  return (await http.getJson<GitHubRelease>(url)).result;
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
@ -1,9 +1,7 @@
 | 
				
			|||||||
import * as download from 'download';
 | 
					 | 
				
			||||||
import * as fs from 'fs';
 | 
					 | 
				
			||||||
import * as os from 'os';
 | 
					import * as os from 'os';
 | 
				
			||||||
import * as path from 'path';
 | 
					import * as path from 'path';
 | 
				
			||||||
import * as util from 'util';
 | 
					import * as util from 'util';
 | 
				
			||||||
import * as restm from 'typed-rest-client/RestClient';
 | 
					import * as github from './github';
 | 
				
			||||||
import * as core from '@actions/core';
 | 
					import * as core from '@actions/core';
 | 
				
			||||||
import * as tc from '@actions/tool-cache';
 | 
					import * as tc from '@actions/tool-cache';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -11,36 +9,37 @@ let osPlat: string = os.platform();
 | 
				
			|||||||
let osArch: string = os.arch();
 | 
					let osArch: string = os.arch();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export async function getUPX(version: string): Promise<string> {
 | 
					export async function getUPX(version: string): Promise<string> {
 | 
				
			||||||
  const selected = await determineVersion(version);
 | 
					  const release: github.GitHubRelease | null = await github.getRelease(version);
 | 
				
			||||||
  if (selected) {
 | 
					  if (!release) {
 | 
				
			||||||
    version = selected;
 | 
					    throw new Error(`Cannot find UPX ${version} release`);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  core.info(`✅ UPX version found: ${version}`);
 | 
					  const semver: string = release.tag_name.replace(/^v/, '');
 | 
				
			||||||
  const tmpdir = fs.mkdtempSync(path.join(os.tmpdir(), 'upx-'));
 | 
					  core.debug(`Semver is ${semver}`);
 | 
				
			||||||
  const fileName = getFileName(version);
 | 
					
 | 
				
			||||||
  const downloadUrl = util.format('https://github.com/upx/upx/releases/download/v%s/%s', version, fileName);
 | 
					  core.info(`✅ UPX version found: ${semver}`);
 | 
				
			||||||
 | 
					  const filename = util.format('%s.%s', getName(semver), osPlat == 'win32' ? 'zip' : 'tar.xz');
 | 
				
			||||||
 | 
					  const downloadUrl = util.format('https://github.com/upx/upx/releases/download/v%s/%s', semver, filename);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  core.info(`⬇️ Downloading ${downloadUrl}...`);
 | 
					  core.info(`⬇️ Downloading ${downloadUrl}...`);
 | 
				
			||||||
  await download.default(downloadUrl, tmpdir, {filename: fileName});
 | 
					  const downloadPath: string = await tc.downloadTool(downloadUrl);
 | 
				
			||||||
 | 
					  core.debug(`Downloaded to ${downloadPath}`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  core.info('📦 Extracting UPX...');
 | 
					  core.info('📦 Extracting UPX...');
 | 
				
			||||||
  let extPath: string = tmpdir;
 | 
					  const extPath: string =
 | 
				
			||||||
  if (osPlat == 'win32') {
 | 
					    osPlat == 'win32' ? await tc.extractZip(downloadPath) : await tc.extractTar(downloadPath, undefined, 'x');
 | 
				
			||||||
    extPath = await tc.extractZip(`${tmpdir}/${fileName}`);
 | 
					  core.debug(`Extracted to ${extPath}`);
 | 
				
			||||||
  } else {
 | 
					 | 
				
			||||||
    extPath = await tc.extractTar(`${tmpdir}/${fileName}`, undefined, 'x');
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  return path.join(extPath, getFileNameWithoutExt(version), osPlat == 'win32' ? 'upx.exe' : 'upx');
 | 
					  const cachePath: string = await tc.cacheDir(extPath, 'ghaction-upx', version);
 | 
				
			||||||
 | 
					  core.debug(`Cached to ${cachePath}`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const exePath: string = path.join(cachePath, getName(semver), osPlat == 'win32' ? 'upx.exe' : 'upx');
 | 
				
			||||||
 | 
					  core.debug(`Exe path is ${exePath}`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return exePath;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
function getFileName(version: string): string {
 | 
					function getName(version: string): string {
 | 
				
			||||||
  const ext: string = osPlat == 'win32' ? 'zip' : 'tar.xz';
 | 
					 | 
				
			||||||
  return util.format('%s.%s', getFileNameWithoutExt(version), ext);
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
function getFileNameWithoutExt(version: string): string {
 | 
					 | 
				
			||||||
  let platform: string = '';
 | 
					  let platform: string = '';
 | 
				
			||||||
  if (osPlat == 'win32') {
 | 
					  if (osPlat == 'win32') {
 | 
				
			||||||
    platform = osArch == 'x64' ? 'win64' : 'win32';
 | 
					    platform = osArch == 'x64' ? 'win64' : 'win32';
 | 
				
			||||||
@ -49,26 +48,3 @@ function getFileNameWithoutExt(version: string): string {
 | 
				
			|||||||
  }
 | 
					  }
 | 
				
			||||||
  return util.format('upx-%s-%s', version, platform);
 | 
					  return util.format('upx-%s-%s', version, platform);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
interface GitHubRelease {
 | 
					 | 
				
			||||||
  tag_name: string;
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
async function determineVersion(version: string): Promise<string> {
 | 
					 | 
				
			||||||
  let rest: restm.RestClient = new restm.RestClient('ghaction-upx', 'https://github.com', undefined, {
 | 
					 | 
				
			||||||
    headers: {
 | 
					 | 
				
			||||||
      Accept: 'application/json'
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
  });
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  if (version !== 'latest') {
 | 
					 | 
				
			||||||
    version = `v${version}`;
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  let res: restm.IRestResponse<GitHubRelease> = await rest.get<GitHubRelease>(`/upx/upx/releases/${version}`);
 | 
					 | 
				
			||||||
  if (res.statusCode != 200 || res.result === null) {
 | 
					 | 
				
			||||||
    throw new Error(`Cannot find UPX ${version} release (http ${res.statusCode})`);
 | 
					 | 
				
			||||||
  }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
  return res.result.tag_name.replace(/^v/, '');
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										16
									
								
								src/main.ts
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								src/main.ts
									
									
									
									
									
								
							@ -1,10 +1,10 @@
 | 
				
			|||||||
import * as installer from './installer';
 | 
					 | 
				
			||||||
import * as fs from 'fs';
 | 
					import * as fs from 'fs';
 | 
				
			||||||
import * as os from 'os';
 | 
					import * as os from 'os';
 | 
				
			||||||
 | 
					import * as installer from './installer';
 | 
				
			||||||
import * as core from '@actions/core';
 | 
					import * as core from '@actions/core';
 | 
				
			||||||
import * as exec from '@actions/exec';
 | 
					import * as exec from '@actions/exec';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export async function run(silent?: boolean) {
 | 
					export async function run() {
 | 
				
			||||||
  try {
 | 
					  try {
 | 
				
			||||||
    if (os.platform() == 'darwin') {
 | 
					    if (os.platform() == 'darwin') {
 | 
				
			||||||
      core.setFailed('Not supported on darwin platform');
 | 
					      core.setFailed('Not supported on darwin platform');
 | 
				
			||||||
@ -14,16 +14,16 @@ export async function run(silent?: boolean) {
 | 
				
			|||||||
    const version = core.getInput('version') || 'latest';
 | 
					    const version = core.getInput('version') || 'latest';
 | 
				
			||||||
    const file = core.getInput('file', {required: true});
 | 
					    const file = core.getInput('file', {required: true});
 | 
				
			||||||
    const args = core.getInput('args');
 | 
					    const args = core.getInput('args');
 | 
				
			||||||
    const upx = await installer.getUPX(version);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if (!fs.existsSync(file)) {
 | 
					    if (!fs.existsSync(file)) {
 | 
				
			||||||
      core.setFailed(`⛔ File to compress not found: ${file}`);
 | 
					      core.setFailed(`File to compress not found: ${file}`);
 | 
				
			||||||
 | 
					      return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    console.log('🏃 Running UPX...');
 | 
					    const upx = await installer.getUPX(version);
 | 
				
			||||||
    await exec.exec(`${upx} ${args} ${file}`, undefined, {
 | 
					
 | 
				
			||||||
      silent: silent
 | 
					    core.info('🏃 Running UPX...');
 | 
				
			||||||
    });
 | 
					    await exec.exec(`${upx} ${args} ${file}`);
 | 
				
			||||||
  } catch (error) {
 | 
					  } catch (error) {
 | 
				
			||||||
    core.setFailed(error.message);
 | 
					    core.setFailed(error.message);
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user