Skip to content

Commit 711ffec

Browse files
committed
fix: fix version with target_platform
close #18 Signed-off-by: DragonBillow <[email protected]>
1 parent 252d069 commit 711ffec

File tree

9 files changed

+237
-113
lines changed

9 files changed

+237
-113
lines changed

src/data_struct/extensions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use serde::{Deserialize, Serialize};
22

33
// https://github.com/microsoft/vscode/blob/d187d50a482ff80dcf74c35affb09dda1a7cd2fe/src/vs/platform/extensions/common/extensions.ts
4-
#[derive(Debug, Serialize, Deserialize)]
4+
#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
55
#[serde(rename_all = "kebab-case")]
66
pub enum TargetPlatform {
77
Win32X64,

src/data_struct/gallery_extension/extension_version.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub struct IRawGalleryExtensionVersion {
1414
pub fallback_asset_uri: String,
1515
pub files: Vec<IRawGalleryExtensionFile>,
1616
pub properties: Vec<IRawGalleryExtensionProperty>,
17-
pub target_platform: String,
17+
pub target_platform: Option<String>,
1818
}
1919

2020
impl Display for IRawGalleryExtensionVersion {

src/dump.rs

+25-19
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ pub async fn dump<'a>(
6969
.into_iter()
7070
.flat_map(|item| item.extensions.into_iter())
7171
.map(|item| async move {
72+
let mut res = vec![];
7273
for version in &item.versions {
7374
match version.get_engine() {
7475
Ok(ver) => {
@@ -121,30 +122,35 @@ pub async fn dump<'a>(
121122
Ok(sha256) => sha256,
122123
Err(err) => {
123124
error!("{err}");
124-
return None;
125+
return vec![];
125126
}
126127
};
127128

128-
return Some(NixContext {
129-
extension_name: item.extension_name.clone(),
130-
publisher_name: item.publisher.publisher_name.clone(),
131-
extension_version: version.version.clone(),
132-
asset_url: if has_asset_url {
133-
Some(asset_url.clone())
134-
} else {
135-
None
136-
},
137-
sha256,
138-
target_platform: client
139-
.get_extension_target_platform(
140-
item.publisher.publisher_name,
141-
item.extension_name,
142-
)
143-
.await,
144-
});
129+
let v: Vec<_> = client
130+
.get_extension_target_platform(
131+
item.publisher.publisher_name.clone(),
132+
item.extension_name.clone(),
133+
)
134+
.await
135+
.into_iter()
136+
.map(|target_platform| NixContext {
137+
extension_name: item.extension_name.clone(),
138+
publisher_name: item.publisher.publisher_name.clone(),
139+
extension_version: version.version.clone(),
140+
asset_url: if has_asset_url {
141+
Some(asset_url.clone())
142+
} else {
143+
None
144+
},
145+
sha256: sha256.clone(),
146+
target_platform,
147+
})
148+
.collect();
149+
150+
res.extend(v);
145151
}
146152

147-
None
153+
res
148154
}),
149155
);
150156
}

src/jinja.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ impl Generator<'_> {
2222
("codelldb", include_str!("./jinja/template/codelldb.j2"));
2323
}
2424

25+
macro_rules! add_filter {
26+
($jinja:ident, $ty:expr) => {
27+
$jinja.add_filter(stringify!($ty), $ty);
28+
};
29+
}
30+
31+
macro_rules! add_function {
32+
($jinja:ident, $ty:expr) => {
33+
$jinja.add_function(stringify!($ty), $ty);
34+
};
35+
}
36+
2537
impl<'a> Generator<'a> {
2638
pub fn new() -> Self {
2739
let mut engine = Environment::new();
@@ -30,8 +42,13 @@ impl<'a> Generator<'a> {
3042
.add_template(Self::NIX_EXPRESSION.0, Self::NIX_EXPRESSION.1)
3143
.unwrap();
3244

33-
engine.add_filter("nixfmt", nixfmt);
34-
engine.add_filter("to_string", to_string);
45+
add_filter!(engine, nixfmt);
46+
add_filter!(engine, to_string);
47+
add_function!(engine, is_universal);
48+
add_function!(engine, is_linux_x86);
49+
add_function!(engine, is_linux_arm);
50+
add_function!(engine, is_darwin_x86);
51+
add_function!(engine, is_darwin_arm);
3552

3653
Self { engine }
3754
}

src/jinja/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ pub struct NixContext {
2121
pub extension_version: String,
2222
pub asset_url: Option<String>,
2323
pub sha256: String,
24-
pub target_platform: Vec<TargetPlatform>,
24+
pub target_platform: TargetPlatform,
2525
}

src/jinja/filters.rs

+26
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,36 @@
11
use minijinja::Value;
22
use nixpkgs_fmt::reformat_string;
33

4+
use crate::data_struct::TargetPlatform;
5+
46
pub fn nixfmt(value: &str) -> String {
57
reformat_string(value)
68
}
79

810
pub fn to_string(value: Value) -> String {
911
format!(r#""{value}""#)
1012
}
13+
14+
pub fn is_universal(target_platform: String) -> bool {
15+
let target_platform: TargetPlatform = target_platform.as_str().into();
16+
matches!(target_platform, TargetPlatform::Universal)
17+
}
18+
19+
pub fn is_linux_x86(target_platform: String) -> bool {
20+
let target_platform: TargetPlatform = target_platform.as_str().into();
21+
matches!(target_platform, TargetPlatform::LinuxX64)
22+
}
23+
24+
pub fn is_linux_arm(target_platform: String) -> bool {
25+
let target_platform: TargetPlatform = target_platform.as_str().into();
26+
matches!(target_platform, TargetPlatform::LinuxArm64)
27+
}
28+
pub fn is_darwin_x86(target_platform: String) -> bool {
29+
let target_platform: TargetPlatform = target_platform.as_str().into();
30+
matches!(target_platform, TargetPlatform::DarwinX64)
31+
}
32+
33+
pub fn is_darwin_arm(target_platform: String) -> bool {
34+
let target_platform: TargetPlatform = target_platform.as_str().into();
35+
matches!(target_platform, TargetPlatform::DarwinArm64)
36+
}

src/jinja/template/nix_expression.nix.j2

+71
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
{ pkgs, lib }:
66

77
let
8+
inherit (pkgs.stdenv) isDarwin isLinux isi686 isx86_64 isAarch32 isAarch64;
89
vscode-utils = pkgs.vscode-utils;
910
in
1011
{
@@ -30,12 +31,82 @@ in
3031
{%- for item in extensions %}
3132
{%- if item.asset_url == None %}
3233
{%- with uniqueId = [ item.publisher_name | to_string, '.', item.extension_name | to_string ] | join %}
34+
{%- if is_universal(item.target_platform) %}
3335
{{ uniqueId }} = vscode-utils.extensionFromVscodeMarketplace {
3436
name = "{{ item.extension_name }}";
3537
publisher = "{{ item.publisher_name }}";
3638
version = "{{ item.extension_version }}";
3739
sha256 = "{{ item.sha256 }}";
3840
};
41+
{%- endif %}
42+
{%- endwith %}
43+
{%- endif %}
44+
{%- endfor %}
45+
}//
46+
lib.attrsets.optionalAttrs(isLinux && (isi686 || isx86_64) ){
47+
{%- for item in extensions %}
48+
{%- if item.asset_url == None %}
49+
{%- with uniqueId = [ item.publisher_name | to_string, '.', item.extension_name | to_string ] | join %}
50+
{%- if is_linux_x86(item.target_platform) %}
51+
{{ uniqueId }} = vscode-utils.extensionFromVscodeMarketplace {
52+
name = "{{ item.extension_name }}";
53+
publisher = "{{ item.publisher_name }}";
54+
version = "{{ item.extension_version }}";
55+
sha256 = "{{ item.sha256 }}";
56+
arch = "linux-x64";
57+
};
58+
{%- endif %}
59+
{%- endwith %}
60+
{%- endif %}
61+
{%- endfor %}
62+
}//
63+
lib.attrsets.optionalAttrs(isLinux && (isAarch32 || isAarch64) ){
64+
{%- for item in extensions %}
65+
{%- if item.asset_url == None %}
66+
{%- with uniqueId = [ item.publisher_name | to_string, '.', item.extension_name | to_string ] | join %}
67+
{%- if is_linux_arm(item.target_platform) %}
68+
{{ uniqueId }} = vscode-utils.extensionFromVscodeMarketplace {
69+
name = "{{ item.extension_name }}";
70+
publisher = "{{ item.publisher_name }}";
71+
version = "{{ item.extension_version }}";
72+
sha256 = "{{ item.sha256 }}";
73+
arch = "linux-arm64";
74+
};
75+
{%- endif %}
76+
{%- endwith %}
77+
{%- endif %}
78+
{%- endfor %}
79+
}//
80+
lib.attrsets.optionalAttrs(isDarwin && (isi686 || isx86_64) ){
81+
{%- for item in extensions %}
82+
{%- if item.asset_url == None %}
83+
{%- with uniqueId = [ item.publisher_name | to_string, '.', item.extension_name | to_string ] | join %}
84+
{%- if is_darwin_x86(item.target_platform) %}
85+
{{ uniqueId }} = vscode-utils.extensionFromVscodeMarketplace {
86+
name = "{{ item.extension_name }}";
87+
publisher = "{{ item.publisher_name }}";
88+
version = "{{ item.extension_version }}";
89+
sha256 = "{{ item.sha256 }}";
90+
arch = "darwin-x64";
91+
};
92+
{%- endif %}
93+
{%- endwith %}
94+
{%- endif %}
95+
{%- endfor %}
96+
}//
97+
lib.attrsets.optionalAttrs(isDarwin && (isAarch32 || isAarch64) ){
98+
{%- for item in extensions %}
99+
{%- if item.asset_url == None %}
100+
{%- with uniqueId = [ item.publisher_name | to_string, '.', item.extension_name | to_string ] | join %}
101+
{%- if is_darwin_arm(item.target_platform) %}
102+
{{ uniqueId }} = vscode-utils.extensionFromVscodeMarketplace {
103+
name = "{{ item.extension_name }}";
104+
publisher = "{{ item.publisher_name }}";
105+
version = "{{ item.extension_version }}";
106+
sha256 = "{{ item.sha256 }}";
107+
arch = "darwin-arm64";
108+
};
109+
{%- endif %}
39110
{%- endwith %}
40111
{%- endif %}
41112
{%- endfor %}

0 commit comments

Comments
 (0)