Skip to content

Commit c664f4d

Browse files
committed
Initial commit: vfox plugin for clang-format
0 parents  commit c664f4d

File tree

9 files changed

+233
-0
lines changed

9 files changed

+233
-0
lines changed

.github/workflows/test.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: Test
2+
3+
on:
4+
push:
5+
branches: [main]
6+
pull_request:
7+
workflow_dispatch:
8+
9+
jobs:
10+
test:
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
os: [ubuntu-latest, macos-latest, windows-latest]
15+
runs-on: ${{ matrix.os }}
16+
steps:
17+
- uses: actions/checkout@v4
18+
19+
- name: Install mise
20+
uses: jdx/mise-action@v2
21+
22+
- name: List available versions
23+
run: mise ls-remote "vfox:${{ github.repository }}" | head -20
24+
25+
- name: Install and test clang-format
26+
run: |
27+
mise install "vfox:${{ github.repository }}@18.1.8"
28+
mise exec "vfox:${{ github.repository }}@18.1.8" -- clang-format --version

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2024 mise-plugins
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# vfox-clang-format
2+
3+
[vfox](https://github.com/version-fox/vfox) plugin for [clang-format](https://clang.llvm.org/docs/ClangFormat.html).
4+
5+
clang-format is a tool to format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# code.
6+
7+
This plugin uses [pixi](https://pixi.sh/) to install pre-built binaries from conda-forge.
8+
9+
## Usage with mise
10+
11+
```bash
12+
# Install a specific version
13+
mise install clang-format@18.1.8
14+
15+
# Use in current shell
16+
mise use clang-format@18.1.8
17+
18+
# Run clang-format
19+
clang-format --version
20+
```
21+
22+
## Usage with vfox
23+
24+
```bash
25+
# Add the plugin
26+
vfox add clang-format
27+
28+
# Install a version
29+
vfox install clang-format@18.1.8
30+
31+
# Use the version
32+
vfox use clang-format@18.1.8
33+
```

hooks/available.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require("util")
2+
3+
function PLUGIN:Available(ctx)
4+
return fetchAvailable()
5+
end

hooks/env_keys.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
function PLUGIN:EnvKeys(ctx)
2+
return {
3+
{
4+
key = "PATH",
5+
value = ctx.path .. "/bin"
6+
}
7+
}
8+
end

hooks/post_install.lua

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require("util")
2+
3+
function PLUGIN:PostInstall(ctx)
4+
pixiInstall(ctx.rootPath, ctx.version)
5+
end

hooks/pre_install.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require("util")
2+
3+
function PLUGIN:PreInstall(ctx)
4+
local file, version = getDownloadInfo(ctx.version)
5+
6+
return {
7+
url = file,
8+
version = version
9+
}
10+
end

lib/util.lua

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
local http = require("http")
2+
local json = require("json")
3+
local env = require("env")
4+
5+
function fetchVersions()
6+
local versionList
7+
local githubURL = os.getenv("GITHUB_URL") or "https://github.com/"
8+
-- Fetch from the same manifest as vfox-clang since clang-format versions match clang
9+
local resp, err = http.get({
10+
url = githubURL:gsub("/$", "") .. "/version-fox/vfox-clang/releases/manifest",
11+
})
12+
if err ~= nil then
13+
error("Failed to request: " .. err)
14+
end
15+
if resp.status_code ~= 200 then
16+
error("Failed to get versions: " .. err .. "\nstatus_code => " .. resp.status_code)
17+
end
18+
19+
versionList = resp.body:match("<code>(.-)</code>")
20+
versionList = json.decode(versionList)["conda-forge"]
21+
22+
return versionList
23+
end
24+
25+
-- available.lua
26+
function fetchAvailable()
27+
local result = {}
28+
29+
for i, v in ipairs(fetchVersions()) do
30+
if i == 1 then
31+
table.insert(result, {
32+
version = v,
33+
note = "latest",
34+
})
35+
else
36+
table.insert(result, {
37+
version = v,
38+
})
39+
end
40+
end
41+
42+
return result
43+
end
44+
45+
-- pre_install.lua
46+
function getDownloadInfo(version)
47+
local file
48+
local ClangVersions = fetchVersions()
49+
50+
if version == "latest" then
51+
version = ClangVersions[1]
52+
end
53+
if not hasValue(ClangVersions, version) then
54+
print("Unsupported version: " .. version)
55+
os.exit(1)
56+
end
57+
file = generatePixi(RUNTIME.osType, RUNTIME.archType)
58+
59+
return file, version
60+
end
61+
62+
function hasValue(table, value)
63+
for _, v in ipairs(table) do
64+
if v == value then
65+
return true
66+
end
67+
end
68+
69+
return false
70+
end
71+
72+
function generatePixi(osType, archType)
73+
local file
74+
local githubURL = os.getenv("GITHUB_URL") or "https://github.com/"
75+
local releaseURL = githubURL:gsub("/$", "") .. "/prefix-dev/pixi/releases/"
76+
77+
if archType == "arm64" then
78+
archType = "aarch64"
79+
elseif archType == "amd64" then
80+
archType = "x86_64"
81+
else
82+
print("Unsupported architecture: " .. archType)
83+
os.exit(1)
84+
end
85+
if osType == "darwin" then
86+
file = "pixi-" .. archType .. "-apple-darwin.tar.gz"
87+
elseif osType == "linux" then
88+
file = "pixi-" .. archType .. "-unknown-linux-musl.tar.gz"
89+
elseif osType == "windows" then
90+
file = "pixi-" .. archType .. "-pc-windows-msvc.zip"
91+
else
92+
print("Unsupported environment: " .. osType .. "-" .. archType)
93+
os.exit(1)
94+
end
95+
file = releaseURL .. "latest/download/" .. file
96+
97+
return file
98+
end
99+
100+
-- post_install.lua
101+
function pixiInstall(path, version)
102+
local condaForge = os.getenv("Conda_Forge") or "conda-forge"
103+
local noStdout = RUNTIME.osType == "windows" and " > nul" or " > /dev/null"
104+
local pixi = RUNTIME.osType == "windows" and path .. "\\pixi.exe" or path .. "/pixi"
105+
local command = pixi .. " global install -qc " .. condaForge .. " clang-format=" .. version
106+
env.setenv("PIXI_HOME", path)
107+
108+
local status = os.execute(command .. noStdout)
109+
if status ~= 0 then
110+
print("Failed to execute command: " .. command)
111+
os.exit(1)
112+
end
113+
os.remove(pixi)
114+
end

metadata.lua

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
PLUGIN = {}
2+
3+
PLUGIN.name = "clang-format"
4+
PLUGIN.version = "0.1.0"
5+
PLUGIN.homepage = "https://github.com/mise-plugins/vfox-clang-format"
6+
PLUGIN.license = "MIT"
7+
PLUGIN.description = "clang-format - format C/C++/Java/JavaScript/JSON/Objective-C/Protobuf/C# code"
8+
PLUGIN.minRuntimeVersion = "0.3.0"
9+
PLUGIN.notes = { "Uses pixi to install from conda-forge" }

0 commit comments

Comments
 (0)