Skip to content

Commit

Permalink
fix: Fix plugin register issue for all platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
hustcer committed Jun 28, 2024
1 parent d0c764e commit 3cc4de3
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 52 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/full-matrix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ on:
- main
- develop
- release/*
- fix/plugins
paths-ignore:
- '**.md'

Expand All @@ -29,8 +30,11 @@ jobs:
uses: hustcer/setup-nu@develop
with:
features: full
enable-plugins: true
version: ${{matrix.ver}}
# Set to `true` will register all plugins
# enable-plugins: true
# You can also set to a comma-separated string of plugin names to register them
enable-plugins: nu_plugin_inc,nu_plugin_query
env:
ACTIONS_STEP_DEBUG: true
- name: Show Nu Version
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
lib
/runner
src/plugins.ts

# Created by https://www.toptal.com/developers/gitignore/api/node
# Edit at https://www.toptal.com/developers/gitignore?templates=node
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ All notable changes to this project will be documented in this file.
### Bug Fixes

- Fix release script of pushing release Tags
- Fix plugin register related issue
- Fix plugin register related issue for all platforms

### Features

Expand Down
1 change: 1 addition & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ build:
$'(ansi p)───────────────────────────────────────(ansi reset)'; \
cd {{SETUP_NU_PATH}}; \
rm -rf dist/*; \
open src/plugins-tpl.ts | str replace __PLUGIN_REGISTER_SCRIPT__ (open nu/register-plugins.nu) | save -rf src/plugins.ts; \
npx ncc build src/index.ts --minify --no-cache; \
$'(ansi g)The `build` task finished!(ansi reset)(char nl)';

Expand Down
56 changes: 51 additions & 5 deletions dist/index.js

Large diffs are not rendered by default.

60 changes: 33 additions & 27 deletions nu/register-plugins.nu
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,42 @@
# 1. https://github.com/actions/runner-images/blob/main/images/win/Windows2022-Readme.md

# Config files are needed to avoid plugin register failure.
# The following lines were used to fix `× Plugin failed to load: No such file or directory (os error 2)`

use common.nu [is-lower-ver]
# The following lines were used to fix "× Plugin failed to load: No such file or directory (os error 2)"

def main [
version: string, # The tag name or version of the release to use.
enablePlugins: string, # Whether to enable or disable plugins.
version: string, # The tag name or version of the release to use.
--is-legacy, # Whether to use the legacy plugin registration command for Nu 0.92.3 and below.
] {

let name = if $version =~ 'nightly' { 'nightly' } else { 'nushell' }
let useRegister = if $version =~ 'nightly' or $version == '*' or (is-lower-ver 0.92.3 $version) { false } else { true }
let config_path = ($nu.env-path | path dirname)
let config_prefix = $'https://github.com/nushell/($name)/blob/($version)/crates/nu-utils/src'
aria2c $'($config_prefix)/sample_config/default_env.nu' -o env.nu -d $config_path
aria2c $'($config_prefix)/sample_config/default_config.nu' -o config.nu -d $config_path
# config reset --without-backup

def register-plugins [] {
ls (which nu | get 0.path | path dirname)
| where name =~ nu_plugin
| each {|plugin|
print $'Registering ($plugin.name)'
if $useRegister {
nu -c $'register ($plugin.name)'
} else {
nu -c $'plugin add ($plugin.name)'
}
let useRegister = if $is_legacy { true } else { false }
let nuDir = (which nu | get 0.path | path dirname)

print 'Output of (which nu):'
print (which nu)
print 'Directory contents:'
ls $nuDir | print

# print $nu
# Create Nu config directory if it does not exist
if not ($nu.default-config-dir | path exists) { mkdir $nu.default-config-dir }
config env --default | save -f $nu.env-path
config nu --default | save -f $nu.config-path
# print (ls $nu.default-config-dir)

let allPlugins = ls $nuDir | where name =~ nu_plugin
let filteredPlugins = if $enablePlugins == 'true' { $allPlugins } else {
$allPlugins | filter {|it| $enablePlugins =~ ($it.name | path basename | split row . | first)}
}

$filteredPlugins | each {|plugin|
let p = $plugin.name | str replace -a \ /
if $useRegister {
echo ([('print "' + $'Registering ($p)' + '"') $'register ($p)'] | str join "\n")
} else {
echo ([('print "' + $'Registering ($p)' + '"') $'plugin add ($p)'] | str join "\n")
}
}
}

register-plugins
print $'(char nl)Plugins registered successfully for Nu ($version).'
| str join "\n"
| save -rf do-register.nu
}

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async function main() {
// Change to workspace directory so that the register-plugins.nu script can be found.
shell.cd(process.env.GITHUB_WORKSPACE);
console.log(`Current directory: ${process.cwd()}`);
registerPlugins(enablePlugins, tool.version);
await registerPlugins(enablePlugins, tool.version);
} catch (err) {
core.setFailed(err.message);
}
Expand Down
35 changes: 35 additions & 0 deletions src/plugins-tpl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import shell from 'shelljs';
import semver from 'semver';
import { promises as fs, constants as fs_constants } from 'fs';

const nu = String.raw;

const pluginRegisterScript = nu`
__PLUGIN_REGISTER_SCRIPT__
`;

export async function registerPlugins(enablePlugins: string, version: string) {
if (enablePlugins === '' || enablePlugins === 'false') {
return;
}
const LEGACY_VERSION = '0.92.3';
const script = 'register-plugins.nu';
const isLegacyVersion = !version.includes('nightly') && semver.lte(version, LEGACY_VERSION);
await fs.writeFile(script, pluginRegisterScript);
try {
await fs.access(script, fs_constants.X_OK);
} catch {
await fs.chmod(script, '755');
console.log(`Fixed file permissions (-> 0o755) for ${script}`);
}
if (isLegacyVersion) {
shell.exec(`nu ${script} "'${enablePlugins}'" ${version} --is-legacy`);
} else {
shell.exec(`nu ${script} "'${enablePlugins}'" ${version}`);
}
console.log('Contents of `do-register.nu`:\n');
const content = shell.cat('do-register.nu');
console.log(content.toString());
shell.exec('nu do-register.nu');
console.log(`Plugins registered successfully for Nu ${version}.`);
}
87 changes: 70 additions & 17 deletions src/plugins.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,79 @@
import path from 'path';
import shell from 'shelljs';
import semver from 'semver';
import { globby } from 'globby';
import { promises as fs, constants as fs_constants } from 'fs';

const nu = String.raw;

const pluginRegisterScript = nu`
#!/usr/bin/env nu
# REF
# 1. https://github.com/actions/runner-images/blob/main/images/win/Windows2022-Readme.md
# Config files are needed to avoid plugin register failure.
# The following lines were used to fix "× Plugin failed to load: No such file or directory (os error 2)"
def main [
enablePlugins: string, # Whether to enable or disable plugins.
version: string, # The tag name or version of the release to use.
--is-legacy, # Whether to use the legacy plugin registration command for Nu 0.92.3 and below.
] {
let useRegister = if $is_legacy { true } else { false }
let nuDir = (which nu | get 0.path | path dirname)
print 'Output of (which nu):'
print (which nu)
print 'Directory contents:'
ls $nuDir | print
# print $nu
# Create Nu config directory if it does not exist
if not ($nu.default-config-dir | path exists) { mkdir $nu.default-config-dir }
config env --default | save -f $nu.env-path
config nu --default | save -f $nu.config-path
# print (ls $nu.default-config-dir)
let allPlugins = ls $nuDir | where name =~ nu_plugin
let filteredPlugins = if $enablePlugins == 'true' { $allPlugins } else {
$allPlugins | filter {|it| $enablePlugins =~ ($it.name | path basename | split row . | first)}
}
$filteredPlugins | each {|plugin|
let p = $plugin.name | str replace -a \ /
if $useRegister {
echo ([('print "' + $'Registering ($p)' + '"') $'register ($p)'] | str join "\n")
} else {
echo ([('print "' + $'Registering ($p)' + '"') $'plugin add ($p)'] | str join "\n")
}
}
| str join "\n"
| save -rf do-register.nu
}
`;

export async function registerPlugins(enablePlugins: string, version: string) {
if (enablePlugins === '' || enablePlugins === 'false') {
return;
}
const LEGACY_VERSION = '0.92.3';
const nuBin = shell.which('nu');
console.log('Nu binary path:', nuBin?.toString());
const nuDir = nuBin ? path.dirname(nuBin.toString()) : '';
const plugins = await globby(`${nuDir}/nu_plugin_*`, { absolute: true, unique: true });
console.log('All available Nu plugins:', plugins);
const filteredPlugins =
enablePlugins === 'true' ? plugins : plugins.filter((it) => enablePlugins.includes(path.basename(it)));
filteredPlugins.forEach((it) => {
console.log(`Register plugin: ${it}`);
if (!version.includes('nightly') && semver.lte(version, LEGACY_VERSION)) {
shell.exec(`nu -c "'register ${it}'"`);
} else {
shell.exec(`nu -c "'plugin add ${it}'"`);
}
});
const script = 'register-plugins.nu';
const isLegacyVersion = !version.includes('nightly') && semver.lte(version, LEGACY_VERSION);
await fs.writeFile(script, pluginRegisterScript);
try {
await fs.access(script, fs_constants.X_OK);
} catch {
await fs.chmod(script, '755');
console.log(`Fixed file permissions (-> 0o755) for ${script}`);
}
if (isLegacyVersion) {
shell.exec(`nu ${script} "'${enablePlugins}'" ${version} --is-legacy`);
} else {
shell.exec(`nu ${script} "'${enablePlugins}'" ${version}`);
}
console.log('Contents of `do-register.nu`:\n');
const content = shell.cat('do-register.nu');
console.log(content.toString());
shell.exec('nu do-register.nu');
console.log(`Plugins registered successfully for Nu ${version}.`);
}

0 comments on commit 3cc4de3

Please sign in to comment.