Skip to content

Commit

Permalink
feat: Add Fig completions (#2986)
Browse files Browse the repository at this point in the history
* Code for Fig completions and publishing

* [autofix.ci] apply automated fixes

* [autofix.ci] apply automated fixes (attempt 2/3)

* Fix task

* Make task depend on completions or it will run in parallel

* Remove hardcoded input path, oops

* Lint fix

* [autofix.ci] apply automated fixes

* Remove autogenerated file

* [autofix.ci] apply automated fixes

* Fix duplicated code

* [autofix.ci] apply automated fixes

* Prettier: Ignore generated mise.ts

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
miguelmig and autofix-ci[bot] authored Nov 11, 2024
1 parent 0277b74 commit 914f668
Show file tree
Hide file tree
Showing 11 changed files with 3,395 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/release-fig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: "Publish mise completions version"
on:
push:
tags:
- "v*" ## Only run the action on new versions, this prevents useless runs of the action

jobs:
push-to-fig-autocomplete:
## if github.repository == 'jdx/mise'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.MY_RELEASE_PLEASE_TOKEN }}
- uses: actions-rust-lang/setup-rust-toolchain@v1
with: { toolchain: nightly, components: rustfmt }
- uses: actions-rust-lang/setup-rust-toolchain@v1
- run: mkdir -p "$HOME/bin" && echo "$HOME/bin" >> "$GITHUB_PATH"
- run: npm i
- run: cargo build --all-features && cp target/debug/mise "$HOME"/bin
- uses: actions/cache/restore@v4
with:
path: |
~/.local/share/mise/installs
~/.local/share/mise/plugins
key: mise-tools-ubuntu-latest-${{ hashFiles('.mise.lock') }}
restore-keys: mise-tools-ubuntu-latest
- run: mise install
- run: mise run render:fig
- name: Create Autocomplete PR ## Create the autocomplete PR using this action
uses: withfig/push-to-fig-autocomplete-action@v2
with:
token: ${{ secrets.MY_RELEASE_PLEASE_TOKEN }}
autocomplete-spec-name: mise
spec-path: tasks/fig/src/mise.ts
pr-body: "Automated PR for latest mise release by https://github.com/jdx/mise"
7 changes: 7 additions & 0 deletions .mise.toml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ depends = ["build"]
env = { NO_COLOR = "1" }
run = "mise render-mangen"

[tasks."render:fig"]
depends = ["build", "render:usage", "render:completions"]
run = [
'usage generate fig --file mise.usage.kdl --out-file tasks/fig/src/mise.ts',
"tsx tasks/fig/addCustomGenerators.ts tasks/fig/src/mise.ts tasks/fig/src/mise.ts"
]

[tasks."render:help"]
depends = ["build"]
env = { NO_COLOR = "1" }
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ docs/.vitepress/dist
docs/public/site.webmanifest
schema/mise.json.hbs
tasks.md
tasks/fig/src/mise.ts
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
"author": "",
"license": "ISC",
"devDependencies": {
"@tsconfig/node18": "^18.2.4",
"@withfig/autocomplete-tools": "^2.10.0",
"@withfig/autocomplete-types": "^1.31.0",
"handlebars": "^4.7.8",
"toml": "^3.0.0",
"ts-pattern": "^5.4.0",
Expand Down
6 changes: 6 additions & 0 deletions tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ User to run as

- **Usage**: `render:completions`

## `render:fig`

- Depends: build, render:usage, render:completions

- **Usage**: `render:fig`

## `render:help`

- Depends: build
Expand Down
1 change: 1 addition & 0 deletions tasks/fig/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build/
131 changes: 131 additions & 0 deletions tasks/fig/addCustomGenerators.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import fsAsync = require("node:fs/promises");
import * as ts from "typescript";
import * as path from "path";

type GeneratorIdentifier = {
identifier: string;
generator_name: string;
};

const customGenerators: GeneratorIdentifier[] = [
{
identifier: "alias",
generator_name: "aliasGenerator",
},
{
identifier: "plugin",
generator_name: "pluginGenerator",
},
{
identifier: "all_plugins",
generator_name: "allPluginsGenerator",
},
{
identifier: "task",
generator_name: "simpleTaskGenerator",
},
{
identifier: "tasks",
generator_name: "simpleTaskGenerator",
},
{
identifier: "setting",
generator_name: "settingsGenerator",
},
{
identifier: "tool@version",
generator_name: "toolVersionGenerator",
},
{
identifier: "installed_tool@version",
generator_name: "installedToolVersionGenerator",
},
{
identifier: "config_file",
generator_name: "configPathGenerator",
},
{
identifier: "env_vars",
generator_name: "envVarGenerator",
},
{
identifier: "tool@version",
generator_name: "toolVersionGenerator",
},
];

const get_identifier = (node: ts.Node): ts.Identifier | undefined => {
let name = "";

const objectLiteralExpr = node as ts.ObjectLiteralExpression;
const properties = objectLiteralExpr.properties;
properties.forEach((p) => {
if (ts.isPropertyAssignment(p) && p.name.getText() == '"name"') {
const value = p.getChildAt(2);
name = value.getText().replaceAll('"', "");
}
});

const custom = customGenerators
.filter((g) => {
if (name === g.identifier) {
return true;
//
}
})
.map((g) => ts.factory.createIdentifier(g.generator_name));

if (custom.length > 0) return custom[0];
return;
};

function transformer<T extends ts.Node>(context: ts.TransformationContext) {
return (rootNode: T) => {
function visit(node: ts.Node): ts.Node {
if (
ts.isPropertyAssignment(node) &&
node.name.getText() === '"generators"'
) {
const id = get_identifier(node.parent);
if (id) {
return ts.factory.updatePropertyAssignment(node, node.name, id);
}
}
return ts.visitEachChild(node, visit, context);
}
return ts.visitNode(rootNode, visit);
};
}

const main = async (fileName: string, outFile?: string) => {
try {
const generatorFileContents = (
await fsAsync.readFile(path.join(__dirname, "generators.ts"))
).toString();
const contents = (await fsAsync.readFile(fileName)).toString();
const sourceFile = ts.createSourceFile(
"example.ts",
contents,
ts.ScriptTarget.Latest,
true,
);
const result = ts.transform(sourceFile, [transformer]);
const transformedSourceFile = result.transformed[0];

const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
const output = printer.printNode(
ts.EmitHint.Unspecified,
transformedSourceFile,
sourceFile,
);

fsAsync.writeFile(
outFile ?? `${fileName.replace(".ts", "")}.out.ts`,
generatorFileContents + "\n" + output,
);
} catch (e) {
console.error(e);
}
};

main(process.argv[2], process.argv[3]);
Loading

0 comments on commit 914f668

Please sign in to comment.