-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.js
146 lines (91 loc) · 3.21 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
const core = require('@actions/core');
const tc = require('@actions/tool-cache');
const io = require('@actions/io');
const exec = require('@actions/exec');
const path = require('path');
const github = require('@actions/github');
// Main entry function
//
async function main(){
try {
// Get inputs
const token = core.getInput('github-token');
var fpmVersion = core.getInput('fpm-version');
console.log(`fpm-version: ${fpmVersion}`);
const fpmRepo = core.getInput('fpm-repository');
console.log(`fpm-repository: ${fpmRepo}`);
// Get latest version if requested
if (fpmVersion === 'latest'){
if (token === 'none') {
core.setFailed('To fetch the latest fpm version, please supply a github token. Alternatively you can specify the fpm release version manually.');
}
try {
fpmVersion = await getLatestReleaseVersion(token);
} catch (error) {
core.setFailed('Error while querying the latest fpm release version - please check your github token.');
}
}
// Build download path
const fetchPath = fpmRepo + '/releases/download/' + fpmVersion + '/';
const filename = getFPMFilename(fpmVersion,process.platform);
console.log(`This platform is ${process.platform}`);
console.log(`Fetching fpm from ${fetchPath}${filename}`);
// Download release
var fpmPath;
try {
fpmPath = await tc.downloadTool(fetchPath+filename);
} catch (error) {
core.setFailed(`Error while trying to fetch fpm - please check that a version exists at the above release url.`);
}
console.log(fpmPath);
const downloadDir = path.dirname(fpmPath);
// Add executable flag on unix
if (process.platform === 'linux' || process.platform === 'darwin'){
await exec.exec('chmod u+x '+fpmPath);
}
// Rename to 'fpm'
if (process.platform === 'win32') {
await io.mv(fpmPath, downloadDir + '/' + 'fpm.exe');
} else {
await io.mv(fpmPath, downloadDir + '/' + 'fpm');
}
// Add to path
core.addPath( downloadDir );
console.log(`fpm added to path at ${downloadDir}`);
} catch (error) {
core.setFailed(error.message);
}
};
// Construct the filename for an fpm release
//
// fpm-<version>-<os>-<arch>[.exe]
//
// <version> is a string of form X.Y.Z corresponding to a release of fpm
// <os> is either 'linux', 'macos', or 'windows'
// <arch> here is always 'x86_64'
//
function getFPMFilename(fpmVersion,platform){
var filename = 'fpm-';
filename += fpmVersion.replace('v','') + '-';
if (platform === 'linux') {
filename += 'linux-x86_64';
} else if (platform === 'darwin') {
filename += 'macos-x86_64';
} else if (platform === 'win32') {
filename += 'windows-x86_64.exe';
} else {
core.setFailed('Unknown platform');
}
return filename;
}
// Query github API to find the tag for the latest release
//
async function getLatestReleaseVersion(token){
const octokit = github.getOctokit(token);
const {data: latest} = await octokit.request('GET /repos/{owner}/{repo}/releases/latest', {
owner: 'fortran-lang',
repo: 'fpm'});
return latest.tag_name;
}
// Call entry function
main();