Skip to content

Commit c62ece8

Browse files
committed
Updated asdf to support linux. Util changes
1 parent f1a0bd2 commit c62ece8

File tree

11 files changed

+80
-36
lines changed

11 files changed

+80
-36
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"ajv": "^8.12.0",
2727
"ajv-formats": "^2.1.1",
2828
"chalk": "^5.3.0",
29-
"codify-plugin-lib": "1.0.182-beta18",
29+
"codify-plugin-lib": "1.0.182-beta21",
3030
"codify-schemas": "1.0.86-beta5",
3131
"debug": "^4.3.4",
3232
"lodash.isequal": "^4.5.0",

scripts/run-tests.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async function launchTestAll(debug: boolean): Promise<void> {
3939

4040
async function launchSingleTest(test: string, debug: boolean) {
4141
console.log(`Running test: ${test}`)
42-
await run(`cirrus run --lazy-pull integration_individual_test -e FILE_NAME="${test}" -o simple`, debug)
42+
await run(`cirrus run --lazy-pull integration_individual_test_linux -e FILE_NAME="${test}" -o simple`, debug)
4343
}
4444

4545
async function run(cmd: string, debug: boolean, simple = true) {

src/resources/asdf/asdf.ts

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import { CreatePlan, DestroyPlan, Resource, ResourceSettings, SpawnStatus, getPty } from 'codify-plugin-lib';
1+
import { CreatePlan, DestroyPlan, Resource, ResourceSettings, SpawnStatus, getPty, FileUtils } from 'codify-plugin-lib';
22
import { OS, ResourceConfig } from 'codify-schemas';
33
import fs from 'node:fs/promises';
44
import os, { homedir } from 'node:os';
55
import path from 'node:path';
66

7-
import { FileUtils } from '../../utils/file-utils.js';
87
import { Utils } from '../../utils/index.js';
98
import AsdfSchema from './asdf-schema.json';
109
import { AsdfPluginsParameter } from './plugins-parameter.js';
@@ -17,7 +16,7 @@ export class AsdfResource extends Resource<AsdfConfig> {
1716
getSettings(): ResourceSettings<AsdfConfig> {
1817
return {
1918
id: 'asdf',
20-
operatingSystems: [OS.Darwin],
19+
operatingSystems: [OS.Darwin, OS.Linux],
2120
schema: AsdfSchema,
2221
parameterSettings: {
2322
plugins: { type: 'stateful', definition: new AsdfPluginsParameter() },
@@ -35,25 +34,67 @@ export class AsdfResource extends Resource<AsdfConfig> {
3534
async create(plan: CreatePlan<AsdfConfig>): Promise<void> {
3635
const $ = getPty();
3736

38-
if (!(await Utils.isHomebrewInstalled())) {
39-
throw new Error('Homebrew is not installed. Please install Homebrew before installing asdf.');
37+
if (Utils.isMacOS()) {
38+
if (!(await Utils.isHomebrewInstalled())) {
39+
throw new Error('Homebrew is not installed. Please install Homebrew before installing asdf.');
40+
}
41+
42+
await $.spawn('brew install asdf', { interactive: true, env: { HOMEBREW_NO_AUTO_UPDATE: 1 } });
43+
4044
}
4145

42-
await $.spawn('brew install asdf', { interactive: true, env: { HOMEBREW_NO_AUTO_UPDATE: 1 } });
46+
if (Utils.isLinux()) {
47+
const { data: latestVersion } = await $.spawn('curl -s https://api.github.com/repos/asdf-vm/asdf/releases/latest | grep \'"tag_name":\' | sed -E \'s/.*"([^"]+)".*/\\1/\'');
4348

44-
await FileUtils.addAllToStartupFile([
45-
'export PATH="${ASDF_DATA_DIR:-$HOME/.asdf}/shims:$PATH"',
46-
]);
47-
}
49+
// Create .asdf directory if it doesn't exist
50+
const asdfDir = path.join(os.homedir(), '.local', 'bin');
51+
await fs.mkdir(asdfDir, { recursive: true });
52+
const tmpDir = await fs.mkdtemp(path.join(os.tmpdir(), 'codify-asdf'));
53+
const arch = Utils.isLinux() ? 'amd64' : 'arm64';
4854

49-
async destroy(plan: DestroyPlan<AsdfConfig>): Promise<void> {
50-
if (!(await Utils.isHomebrewInstalled())) {
51-
return;
55+
// Download and extract asdf
56+
await $.spawn(`curl -Lo ${tmpDir}/asdf.tar.gz "https://github.com/asdf-vm/asdf/releases/download/${latestVersion}/asdf-${latestVersion}-linux-${arch}.tar.gz"`, { cwd: tmpDir });
57+
console.log(await $.spawn('ls -la', { cwd: tmpDir }));
58+
await $.spawn(`tar -xzf ${tmpDir}/asdf.tar.gz -C ${asdfDir}`, { cwd: tmpDir });
59+
await fs.chmod(path.join(asdfDir, 'asdf'), 0o755);
60+
61+
await fs.rm(tmpDir, { recursive: true, force: true });
5262
}
5363

64+
await FileUtils.addPathToShellRc(path.join(os.homedir(), '.local', 'bin'), true);
65+
// eslint-disable-next-line no-template-curly-in-string
66+
await FileUtils.addToShellRc('export PATH="${ASDF_DATA_DIR:-$HOME/.asdf}/shims:$PATH"')
67+
68+
// TODO: Add filtering to resources for operating systems
69+
// os parameter
70+
// TODO: Move all utils to codify-plugin-lib
71+
// TODO: Move OsUtils to a separate name space? All things that have to do with the os.
72+
// TODO: Add a way to run multiple commands in sequence
73+
// TODO: Add a easier way to make sure path is on the path
74+
// TODO: Change all plugins to install to ~/.local/bin
75+
76+
await $.spawnSafe('which asdf', { interactive: true });
77+
await $.spawnSafe('ls ~/.local/bin');
78+
console.log((await $.spawnSafe('echo $PATH', { interactive: true })).data);
79+
80+
}
81+
82+
async destroy(): Promise<void> {
5483
const $ = getPty();
55-
await $.spawn('brew uninstall asdf', { interactive: true, env: { HOMEBREW_NO_AUTO_UPDATE: 1 } });
56-
await FileUtils.removeLineFromStartupFile('export PATH="${ASDF_DATA_DIR:-$HOME/.asdf}/shims:$PATH"')
84+
85+
const asdfDir = (await $.spawn('which asdf', { interactive: true })).data;
86+
if (Utils.isMacOS() && asdfDir.includes('homebrew')) {
87+
if (!(await Utils.isHomebrewInstalled())) {
88+
return;
89+
}
90+
91+
await $.spawn('brew uninstall asdf', { interactive: true, env: { HOMEBREW_NO_AUTO_UPDATE: 1 } });
92+
} else {
93+
await fs.rm(asdfDir, { recursive: true, force: true });
94+
}
95+
96+
// eslint-disable-next-line no-template-curly-in-string
97+
await FileUtils.removeLineFromShellRc('export PATH="${ASDF_DATA_DIR:-$HOME/.asdf}/shims:$PATH"')
5798
await fs.rm(path.join(os.homedir(), '.asdf'), { recursive: true, force: true });
5899
}
59100

src/resources/docker/docker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export class DockerResource extends Resource<DockerConfig> {
8888
}
8989

9090
await $.spawn('xattr -r -d com.apple.quarantine /Applications/Docker.app', { requiresRoot: true });
91-
await FileUtils.addPathToZshrc('/Applications/Docker.app/Contents/Resources/bin', false);
91+
await FileUtils.addPathToPrimaryShellRc('/Applications/Docker.app/Contents/Resources/bin', false);
9292
}
9393

9494
async destroy(plan: DestroyPlan<DockerConfig>): Promise<void> {

src/resources/java/jenv/jenv.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import { getPty, Resource, ResourceSettings, SpawnStatus } from 'codify-plugin-lib';
2-
import { ResourceConfig } from 'codify-schemas';
1+
import { Resource, ResourceSettings, SpawnStatus, getPty } from 'codify-plugin-lib';
2+
import { OS, ResourceConfig } from 'codify-schemas';
33
import * as fs from 'node:fs';
44

55
import { FileUtils } from '../../../utils/file-utils.js';
66
import { JenvGlobalParameter } from './global-parameter.js';
77
import {
8-
JAVA_VERSION_INTEGER,
98
JenvAddParameter,
109
OPENJDK_SUPPORTED_VERSIONS
1110
} from './java-versions-parameter.js';
@@ -20,6 +19,7 @@ export class JenvResource extends Resource<JenvConfig> {
2019
getSettings(): ResourceSettings<JenvConfig> {
2120
return {
2221
id: 'jenv',
22+
operatingSystems: [OS.Darwin],
2323
schema: Schema,
2424
dependencies: ['homebrew'],
2525
parameterSettings: {

src/resources/shell/path/path-resource.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ export class PathResource extends Resource<PathConfig> {
193193

194194
private async addPath(path: string, prepend = false): Promise<void> {
195195
// Escaping is done within file utils
196-
await FileUtils.addPathToZshrc(path, prepend);
196+
await FileUtils.addPathToPrimaryShellRc(path, prepend);
197197
}
198198

199199
private async removePath(pathValue: string): Promise<void> {

src/resources/terraform/terraform.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ ${JSON.stringify(releaseInfo, null, 2)}
9898
await $.spawn(`mv ./terraform ${directory}`, { cwd: temporaryDir, requiresRoot: true })
9999
await $.spawn(`rm -rf ${temporaryDir}`)
100100

101-
if (!await Utils.isDirectoryOnPath(directory)) {
101+
if (!(await Utils.isDirectoryOnPath(directory))) {
102102
await FileUtils.addToStartupFile(`export PATH=$PATH:${directory}`);
103103
}
104104
}

src/utils/file-utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ export class FileUtils {
2828
}
2929
}
3030

31-
static async addAllToStartupFile(lines: string[]): Promise<void> {
32-
const formattedLines = '\n' + lines.join('\n') + '\n';
31+
static async addAllToStartupFile(lines: (string | undefined)[]): Promise<void> {
32+
const formattedLines = '\n' + lines.filter(Boolean).join('\n') + '\n';
3333
const shellRc = Utils.getPrimaryShellRc();
3434

3535
console.log(`Adding to ${path.basename(shellRc)}:
@@ -38,7 +38,7 @@ ${lines.join('\n')}`)
3838
await fs.appendFile(shellRc, formattedLines)
3939
}
4040

41-
static async addPathToZshrc(value: string, prepend: boolean): Promise<void> {
41+
static async addPathToPrimaryShellRc(value: string, prepend: boolean): Promise<void> {
4242
const shellRc = Utils.getPrimaryShellRc();
4343
console.log(`Saving path: ${value} to ${shellRc}`);
4444

src/utils/index.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getPty } from 'codify-plugin-lib';
12
import * as fsSync from 'node:fs';
23
import * as fs from 'node:fs/promises';
34
import os from 'node:os';
@@ -83,8 +84,10 @@ export const Utils = {
8384
},
8485

8586
async isDirectoryOnPath(directory: string): Promise<boolean> {
86-
const pathQuery = await codifySpawn('echo $PATH');
87-
return pathQuery.data.includes(directory);
87+
const $ = getPty();
88+
const { data: pathQuery } = await $.spawn('echo $PATH', { interactive: true });
89+
const lines = pathQuery.split(':');
90+
return lines.includes(directory);
8891
},
8992

9093
async isHomebrewInstalled(): Promise<boolean> {

0 commit comments

Comments
 (0)