Skip to content

Commit 7bee105

Browse files
Parse inputs in main.ts
1 parent a709f1e commit 7bee105

File tree

8 files changed

+115
-73
lines changed

8 files changed

+115
-73
lines changed

__tests__/find-haskell.test.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ describe('haskell/actions/setup', () => {
4646
it('Setting disable-matcher to true disables matcher', () => {
4747
forAllOS(os => {
4848
const options = getOpts(def(os), os, {
49-
'disable-matcher': 'true'
49+
disableMatcher: 'true'
5050
});
5151
expect(options.general.matcher.enable).toBe(false);
5252
});
@@ -70,10 +70,10 @@ describe('haskell/actions/setup', () => {
7070
const v = {ghc: '8.6.5', cabal: '3.4.1.0', stack: '1.9.3'};
7171
forAllOS(os => {
7272
const options = getOpts(def(os), os, {
73-
'enable-stack': 'true',
74-
'stack-version': '1',
75-
'ghc-version': '8.6',
76-
'cabal-version': '3.4'
73+
enableStack: 'true',
74+
stackVersion: '1',
75+
ghcVersion: '8.6',
76+
cabalVersion: '3.4'
7777
});
7878
forAllTools(t => expect(options[t].resolved).toBe(v[t]));
7979
});
@@ -82,10 +82,10 @@ describe('haskell/actions/setup', () => {
8282
it('"latest" Versions resolve correctly', () => {
8383
forAllOS(os => {
8484
const options = getOpts(def(os), os, {
85-
'enable-stack': 'true',
86-
'stack-version': 'latest',
87-
'ghc-version': 'latest',
88-
'cabal-version': 'latest'
85+
enableStack: 'true',
86+
stackVersion: 'latest',
87+
ghcVersion: 'latest',
88+
cabalVersion: 'latest'
8989
});
9090
forAllTools(t =>
9191
expect(options[t].resolved).toBe(
@@ -100,10 +100,10 @@ describe('haskell/actions/setup', () => {
100100
const v = {ghc: '8.10.7', cabal: '2.4.1.0', stack: '2.1.3'};
101101
forAllOS(os => {
102102
const options = getOpts(def(os), os, {
103-
'enable-stack': 'true',
104-
'stack-version': '2.1',
105-
'ghc-version': '8.10',
106-
'cabal-version': '2'
103+
enableStack: 'true',
104+
stackVersion: '2.1',
105+
ghcVersion: '8.10',
106+
cabalVersion: '2'
107107
});
108108
forAllTools(t => expect(options[t].resolved).toBe(v[t]));
109109
});
@@ -112,7 +112,7 @@ describe('haskell/actions/setup', () => {
112112
it('Enabling stack does not disable GHC or Cabal', () => {
113113
forAllOS(os => {
114114
const {ghc, cabal, stack} = getOpts(def(os), os, {
115-
'enable-stack': 'true'
115+
enableStack: 'true'
116116
});
117117
expect({
118118
ghc: ghc.enable,
@@ -125,20 +125,20 @@ describe('haskell/actions/setup', () => {
125125
it('Resolves revisions correctly on Windows', () => {
126126
// Test the case where there is a revision in chocolatey
127127
expect(
128-
getOpts(def('win32'), 'win32', {'ghc-version': '8.10.2'}).ghc.resolved
128+
getOpts(def('win32'), 'win32', {ghcVersion: '8.10.2'}).ghc.resolved
129129
).toBe('8.10.2'); // Andreas, 2022-12-29: revisions are handled locally in choco() now
130130

131131
// Test the case where there is not a revision in chocolatey
132132
expect(
133-
getOpts(def('win32'), 'win32', {'ghc-version': '8.8.1'}).ghc.resolved
133+
getOpts(def('win32'), 'win32', {ghcVersion: '8.8.1'}).ghc.resolved
134134
).toBe('8.8.1');
135135
});
136136

137137
it('Enabling stack-no-global disables GHC and Cabal', () => {
138138
forAllOS(os => {
139139
const {ghc, cabal, stack} = getOpts(def(os), os, {
140-
'enable-stack': 'true',
141-
'stack-no-global': 'true'
140+
enableStack: 'true',
141+
stackNoGlobal: 'true'
142142
});
143143
expect({
144144
ghc: ghc.enable,
@@ -150,13 +150,13 @@ describe('haskell/actions/setup', () => {
150150

151151
it('Enabling stack-no-global without setting enable-stack errors', () => {
152152
forAllOS(os =>
153-
expect(() => getOpts(def(os), os, {'stack-no-global': 'true'})).toThrow()
153+
expect(() => getOpts(def(os), os, {stackNoGlobal: 'true'})).toThrow()
154154
);
155155
});
156156

157157
it('Enabling stack-setup-ghc without setting enable-stack errors', () => {
158158
forAllOS(os =>
159-
expect(() => getOpts(def(os), os, {'stack-setup-ghc': 'true'})).toThrow()
159+
expect(() => getOpts(def(os), os, {stackNoGlobal: 'true'})).toThrow()
160160
);
161161
});
162162
});

dist/index.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13629,9 +13629,19 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
1362913629
};
1363013630
Object.defineProperty(exports, "__esModule", ({ value: true }));
1363113631
const core = __importStar(__nccwpck_require__(2186));
13632-
const opts_1 = __nccwpck_require__(8131);
1363313632
const setup_haskell_1 = __importDefault(__nccwpck_require__(9351));
13634-
(0, setup_haskell_1.default)(Object.fromEntries(Object.keys(opts_1.yamlInputs).map(k => [k, core.getInput(k)])));
13633+
(0, setup_haskell_1.default)({
13634+
ghcVersion: core.getInput('ghc-version'),
13635+
cabalVersion: core.getInput('cabal-version'),
13636+
stackVersion: core.getInput('stack-version'),
13637+
enableStack: core.getInput('enable-stack'),
13638+
stackNoGlobal: core.getInput('stack-no-global'),
13639+
stackSetupGhc: core.getInput('stack-setup-ghc'),
13640+
cabalUpdate: core.getInput('cabal-update'),
13641+
ghcupReleaseChannels: core.getInput('ghcup-release-channels'),
13642+
ghcupReleaseChannel: core.getInput('ghcup-release-channel'),
13643+
disableMatcher: core.getInput('disable-matcher')
13644+
});
1363513645

1363613646

1363713647
/***/ }),
@@ -13776,15 +13786,15 @@ function parseCSV(val) {
1377613786
}
1377713787
function getOpts({ ghc, cabal, stack }, os, inputs) {
1377813788
core.debug(`Inputs are: ${JSON.stringify(inputs)}`);
13779-
const stackNoGlobal = (inputs['stack-no-global'] || '') !== '';
13780-
const stackSetupGhc = (inputs['stack-setup-ghc'] || '') !== '';
13781-
const stackEnable = (inputs['enable-stack'] || '') !== '';
13782-
const matcherDisable = (inputs['disable-matcher'] || '') !== '';
13783-
if (inputs['ghcup-release-channel']) {
13789+
const stackNoGlobal = (inputs.stackNoGlobal ?? '') !== '';
13790+
const stackSetupGhc = (inputs.stackSetupGhc ?? '') !== '';
13791+
const stackEnable = (inputs.enableStack ?? '') !== '';
13792+
const matcherDisable = (inputs.disableMatcher ?? '') !== '';
13793+
if (inputs.ghcupReleaseChannel) {
1378413794
core.warning('ghcup-release-channel is deprecated in favor of ghcup-release-channels');
13785-
inputs['ghcup-release-channels'] = inputs['ghcup-release-channel'];
13795+
inputs.ghcupReleaseChannels = inputs.ghcupReleaseChannel;
1378613796
}
13787-
const ghcupReleaseChannels = parseCSV(inputs['ghcup-release-channels'] ?? '').map(v => {
13797+
const ghcupReleaseChannels = parseCSV(inputs.ghcupReleaseChannels ?? '').map(v => {
1378813798
try {
1378913799
return new URL(v);
1379013800
}
@@ -13796,12 +13806,12 @@ function getOpts({ ghc, cabal, stack }, os, inputs) {
1379613806
// 'cabal-update' has a default value, so we should get a proper boolean always.
1379713807
// Andreas, 2023-01-06: This is not true if we use the action as a library.
1379813808
// Thus, need to patch with default value here.
13799-
const cabalUpdate = parseYAMLBoolean('cabal-update', inputs['cabal-update'] || 'true');
13809+
const cabalUpdate = parseYAMLBoolean('cabal-update', inputs.cabalUpdate ?? 'true');
1380013810
core.debug(`${stackNoGlobal}/${stackSetupGhc}/${stackEnable}`);
1380113811
const verInpt = {
13802-
ghc: inputs['ghc-version'] || ghc.version,
13803-
cabal: inputs['cabal-version'] || cabal.version,
13804-
stack: inputs['stack-version'] || stack.version
13812+
ghc: inputs.ghcVersion || ghc.version,
13813+
cabal: inputs.cabalVersion || cabal.version,
13814+
stack: inputs.stackVersion || stack.version
1380513815
};
1380613816
const errors = [];
1380713817
if (stackNoGlobal && !stackEnable) {

lib/opts.d.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,17 @@ export declare function releaseRevision(version: string, tool: Tool, os: OS): st
8585
* @returns boolean
8686
*/
8787
export declare function parseYAMLBoolean(name: string, val: string): boolean;
88-
export declare function getOpts({ ghc, cabal, stack }: Defaults, os: OS, inputs: Record<string, string>): Options;
88+
export type RawInputs = {
89+
ghcVersion?: string;
90+
cabalVersion?: string;
91+
stackVersion?: string;
92+
enableStack?: string;
93+
stackNoGlobal?: string;
94+
stackSetupGhc?: string;
95+
cabalUpdate?: string;
96+
ghcupReleaseChannels?: string;
97+
ghcupReleaseChannel?: string;
98+
disableMatcher?: string;
99+
};
100+
export declare function getOpts({ ghc, cabal, stack }: Defaults, os: OS, inputs: RawInputs): Options;
89101
export {};

lib/opts.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,15 @@ function parseCSV(val) {
134134
}
135135
function getOpts({ ghc, cabal, stack }, os, inputs) {
136136
core.debug(`Inputs are: ${JSON.stringify(inputs)}`);
137-
const stackNoGlobal = (inputs['stack-no-global'] || '') !== '';
138-
const stackSetupGhc = (inputs['stack-setup-ghc'] || '') !== '';
139-
const stackEnable = (inputs['enable-stack'] || '') !== '';
140-
const matcherDisable = (inputs['disable-matcher'] || '') !== '';
141-
if (inputs['ghcup-release-channel']) {
137+
const stackNoGlobal = (inputs.stackNoGlobal ?? '') !== '';
138+
const stackSetupGhc = (inputs.stackSetupGhc ?? '') !== '';
139+
const stackEnable = (inputs.enableStack ?? '') !== '';
140+
const matcherDisable = (inputs.disableMatcher ?? '') !== '';
141+
if (inputs.ghcupReleaseChannel) {
142142
core.warning('ghcup-release-channel is deprecated in favor of ghcup-release-channels');
143-
inputs['ghcup-release-channels'] = inputs['ghcup-release-channel'];
143+
inputs.ghcupReleaseChannels = inputs.ghcupReleaseChannel;
144144
}
145-
const ghcupReleaseChannels = parseCSV(inputs['ghcup-release-channels'] ?? '').map(v => {
145+
const ghcupReleaseChannels = parseCSV(inputs.ghcupReleaseChannels ?? '').map(v => {
146146
try {
147147
return new URL(v);
148148
}
@@ -154,12 +154,12 @@ function getOpts({ ghc, cabal, stack }, os, inputs) {
154154
// 'cabal-update' has a default value, so we should get a proper boolean always.
155155
// Andreas, 2023-01-06: This is not true if we use the action as a library.
156156
// Thus, need to patch with default value here.
157-
const cabalUpdate = parseYAMLBoolean('cabal-update', inputs['cabal-update'] || 'true');
157+
const cabalUpdate = parseYAMLBoolean('cabal-update', inputs.cabalUpdate ?? 'true');
158158
core.debug(`${stackNoGlobal}/${stackSetupGhc}/${stackEnable}`);
159159
const verInpt = {
160-
ghc: inputs['ghc-version'] || ghc.version,
161-
cabal: inputs['cabal-version'] || cabal.version,
162-
stack: inputs['stack-version'] || stack.version
160+
ghc: inputs.ghcVersion || ghc.version,
161+
cabal: inputs.cabalVersion || cabal.version,
162+
stack: inputs.stackVersion || stack.version
163163
};
164164
const errors = [];
165165
if (stackNoGlobal && !stackEnable) {

lib/setup-haskell.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export default function run(inputs: Record<string, string>): Promise<void>;
1+
import { RawInputs } from './opts';
2+
export default function run(inputs: RawInputs): Promise<void>;

src/main.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
import * as core from '@actions/core';
2-
import {yamlInputs} from './opts';
32
import run from './setup-haskell';
43

5-
run(
6-
Object.fromEntries(Object.keys(yamlInputs).map(k => [k, core.getInput(k)]))
7-
);
4+
run({
5+
ghcVersion: core.getInput('ghc-version'),
6+
cabalVersion: core.getInput('cabal-version'),
7+
stackVersion: core.getInput('stack-version'),
8+
enableStack: core.getInput('enable-stack'),
9+
stackNoGlobal: core.getInput('stack-no-global'),
10+
stackSetupGhc: core.getInput('stack-setup-ghc'),
11+
cabalUpdate: core.getInput('cabal-update'),
12+
ghcupReleaseChannels: core.getInput('ghcup-release-channels'),
13+
ghcupReleaseChannel: core.getInput('ghcup-release-channel'),
14+
disableMatcher: core.getInput('disable-matcher')
15+
});

src/opts.ts

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -148,47 +148,60 @@ function parseCSV(val: string): string[] {
148148
.filter(s => s != '');
149149
}
150150

151+
export type RawInputs = {
152+
ghcVersion?: string;
153+
cabalVersion?: string;
154+
stackVersion?: string;
155+
enableStack?: string;
156+
stackNoGlobal?: string;
157+
stackSetupGhc?: string;
158+
cabalUpdate?: string;
159+
ghcupReleaseChannels?: string;
160+
ghcupReleaseChannel?: string;
161+
disableMatcher?: string;
162+
};
163+
151164
export function getOpts(
152165
{ghc, cabal, stack}: Defaults,
153166
os: OS,
154-
inputs: Record<string, string>
167+
inputs: RawInputs
155168
): Options {
156169
core.debug(`Inputs are: ${JSON.stringify(inputs)}`);
157-
const stackNoGlobal = (inputs['stack-no-global'] || '') !== '';
158-
const stackSetupGhc = (inputs['stack-setup-ghc'] || '') !== '';
159-
const stackEnable = (inputs['enable-stack'] || '') !== '';
160-
const matcherDisable = (inputs['disable-matcher'] || '') !== '';
170+
const stackNoGlobal = (inputs.stackNoGlobal ?? '') !== '';
171+
const stackSetupGhc = (inputs.stackSetupGhc ?? '') !== '';
172+
const stackEnable = (inputs.enableStack ?? '') !== '';
173+
const matcherDisable = (inputs.disableMatcher ?? '') !== '';
161174

162-
if (inputs['ghcup-release-channel']) {
175+
if (inputs.ghcupReleaseChannel) {
163176
core.warning(
164177
'ghcup-release-channel is deprecated in favor of ghcup-release-channels'
165178
);
166-
inputs['ghcup-release-channels'] = inputs['ghcup-release-channel'];
179+
inputs.ghcupReleaseChannels = inputs.ghcupReleaseChannel;
167180
}
168181

169-
const ghcupReleaseChannels = parseCSV(
170-
inputs['ghcup-release-channels'] ?? ''
171-
).map(v => {
172-
try {
173-
return new URL(v);
174-
} catch (e) {
175-
throw new TypeError(`Not a valid URL: ${v}`);
182+
const ghcupReleaseChannels = parseCSV(inputs.ghcupReleaseChannels ?? '').map(
183+
v => {
184+
try {
185+
return new URL(v);
186+
} catch (e) {
187+
throw new TypeError(`Not a valid URL: ${v}`);
188+
}
176189
}
177-
});
190+
);
178191

179192
// Andreas, 2023-01-05, issue #29:
180193
// 'cabal-update' has a default value, so we should get a proper boolean always.
181194
// Andreas, 2023-01-06: This is not true if we use the action as a library.
182195
// Thus, need to patch with default value here.
183196
const cabalUpdate = parseYAMLBoolean(
184197
'cabal-update',
185-
inputs['cabal-update'] || 'true'
198+
inputs.cabalUpdate ?? 'true'
186199
);
187200
core.debug(`${stackNoGlobal}/${stackSetupGhc}/${stackEnable}`);
188201
const verInpt = {
189-
ghc: inputs['ghc-version'] || ghc.version,
190-
cabal: inputs['cabal-version'] || cabal.version,
191-
stack: inputs['stack-version'] || stack.version
202+
ghc: inputs.ghcVersion || ghc.version,
203+
cabal: inputs.cabalVersion || cabal.version,
204+
stack: inputs.stackVersion || stack.version
192205
};
193206

194207
const errors = [];

src/setup-haskell.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import ensureError from 'ensure-error';
44
import * as fs from 'fs';
55
import * as path from 'path';
66
import {EOL} from 'os';
7-
import {getOpts, getDefaults, Tool} from './opts';
7+
import {RawInputs, getOpts, getDefaults, Tool} from './opts';
88
import {addGhcupReleaseChannel, installTool, resetTool} from './installer';
99
import type {OS} from './opts';
1010
import {exec} from '@actions/exec';
@@ -19,9 +19,7 @@ async function cabalConfig(): Promise<string> {
1919
return out.toString().trim().split('\n').slice(-1)[0].trim();
2020
}
2121

22-
export default async function run(
23-
inputs: Record<string, string>
24-
): Promise<void> {
22+
export default async function run(inputs: RawInputs): Promise<void> {
2523
try {
2624
core.info('Preparing to setup a Haskell environment');
2725
const os = process.platform as OS;

0 commit comments

Comments
 (0)