Skip to content

Commit

Permalink
fix: fix version with target_platform
Browse files Browse the repository at this point in the history
close #18

Signed-off-by: DragonBillow <[email protected]>
  • Loading branch information
cathaysia committed Mar 9, 2024
1 parent 457bd90 commit dd1c636
Show file tree
Hide file tree
Showing 9 changed files with 237 additions and 113 deletions.
2 changes: 1 addition & 1 deletion src/data_struct/extensions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};

// https://github.com/microsoft/vscode/blob/d187d50a482ff80dcf74c35affb09dda1a7cd2fe/src/vs/platform/extensions/common/extensions.ts
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, Clone, Copy)]
#[serde(rename_all = "kebab-case")]
pub enum TargetPlatform {
Win32X64,
Expand Down
2 changes: 1 addition & 1 deletion src/data_struct/gallery_extension/extension_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct IRawGalleryExtensionVersion {
pub fallback_asset_uri: String,
pub files: Vec<IRawGalleryExtensionFile>,
pub properties: Vec<IRawGalleryExtensionProperty>,
pub target_platform: String,
pub target_platform: Option<String>,
}

impl Display for IRawGalleryExtensionVersion {
Expand Down
44 changes: 25 additions & 19 deletions src/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub async fn dump<'a>(
.into_iter()
.flat_map(|item| item.extensions.into_iter())
.map(|item| async move {
let mut res = vec![];
for version in &item.versions {
match version.get_engine() {
Ok(ver) => {
Expand Down Expand Up @@ -121,30 +122,35 @@ pub async fn dump<'a>(
Ok(sha256) => sha256,
Err(err) => {
error!("{err}");
return None;
return vec![];
}
};

return Some(NixContext {
extension_name: item.extension_name.clone(),
publisher_name: item.publisher.publisher_name.clone(),
extension_version: version.version.clone(),
asset_url: if has_asset_url {
Some(asset_url.clone())
} else {
None
},
sha256,
target_platform: client
.get_extension_target_platform(
item.publisher.publisher_name,
item.extension_name,
)
.await,
});
let v: Vec<_> = client
.get_extension_target_platform(
item.publisher.publisher_name.clone(),
item.extension_name.clone(),
)
.await
.into_iter()
.map(|target_platform| NixContext {
extension_name: item.extension_name.clone(),
publisher_name: item.publisher.publisher_name.clone(),
extension_version: version.version.clone(),
asset_url: if has_asset_url {
Some(asset_url.clone())
} else {
None
},
sha256: sha256.clone(),
target_platform,
})
.collect();

res.extend(v);
}

None
res
}),
);
}
Expand Down
21 changes: 19 additions & 2 deletions src/jinja.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,18 @@ impl Generator<'_> {
("codelldb", include_str!("./jinja/template/codelldb.j2"));
}

macro_rules! add_filter {
($jinja:ident, $ty:expr) => {
$jinja.add_filter(stringify!($ty), $ty);
};
}

macro_rules! add_function {
($jinja:ident, $ty:expr) => {
$jinja.add_function(stringify!($ty), $ty);
};
}

impl<'a> Generator<'a> {
pub fn new() -> Self {
let mut engine = Environment::new();
Expand All @@ -30,8 +42,13 @@ impl<'a> Generator<'a> {
.add_template(Self::NIX_EXPRESSION.0, Self::NIX_EXPRESSION.1)
.unwrap();

engine.add_filter("nixfmt", nixfmt);
engine.add_filter("to_string", to_string);
add_filter!(engine, nixfmt);
add_filter!(engine, to_string);
add_function!(engine, is_universal);
add_function!(engine, is_linux_x86);
add_function!(engine, is_linux_arm);
add_function!(engine, is_darwin_x86);
add_function!(engine, is_darwin_arm);

Self { engine }
}
Expand Down
2 changes: 1 addition & 1 deletion src/jinja/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ pub struct NixContext {
pub extension_version: String,
pub asset_url: Option<String>,
pub sha256: String,
pub target_platform: Vec<TargetPlatform>,
pub target_platform: TargetPlatform,
}
26 changes: 26 additions & 0 deletions src/jinja/filters.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
use minijinja::Value;
use nixpkgs_fmt::reformat_string;

use crate::data_struct::TargetPlatform;

pub fn nixfmt(value: &str) -> String {
reformat_string(value)
}

pub fn to_string(value: Value) -> String {
format!(r#""{value}""#)
}

pub fn is_universal(target_platform: String) -> bool {
let target_platform: TargetPlatform = target_platform.as_str().into();
matches!(target_platform, TargetPlatform::Universal)
}

pub fn is_linux_x86(target_platform: String) -> bool {
let target_platform: TargetPlatform = target_platform.as_str().into();
matches!(target_platform, TargetPlatform::LinuxX64)
}

pub fn is_linux_arm(target_platform: String) -> bool {
let target_platform: TargetPlatform = target_platform.as_str().into();
matches!(target_platform, TargetPlatform::LinuxArm64)
}
pub fn is_darwin_x86(target_platform: String) -> bool {
let target_platform: TargetPlatform = target_platform.as_str().into();
matches!(target_platform, TargetPlatform::DarwinX64)
}

pub fn is_darwin_arm(target_platform: String) -> bool {
let target_platform: TargetPlatform = target_platform.as_str().into();
matches!(target_platform, TargetPlatform::DarwinArm64)
}
71 changes: 71 additions & 0 deletions src/jinja/template/nix_expression.nix.j2
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
{ pkgs, lib }:

let
inherit (pkgs.stdenv) isDarwin isLinux isi686 isx86_64 isAarch32 isAarch64;
vscode-utils = pkgs.vscode-utils;
in
{
Expand All @@ -30,12 +31,82 @@ in
{%- for item in extensions %}
{%- if item.asset_url == None %}
{%- with uniqueId = [ item.publisher_name | to_string, '.', item.extension_name | to_string ] | join %}
{%- if is_universal(item.target_platform) %}
{{ uniqueId }} = vscode-utils.extensionFromVscodeMarketplace {
name = "{{ item.extension_name }}";
publisher = "{{ item.publisher_name }}";
version = "{{ item.extension_version }}";
sha256 = "{{ item.sha256 }}";
};
{%- endif %}
{%- endwith %}
{%- endif %}
{%- endfor %}
}//
lib.attrsets.optionalAttrs(isLinux && (isi686 || isx86_64) ){
{%- for item in extensions %}
{%- if item.asset_url == None %}
{%- with uniqueId = [ item.publisher_name | to_string, '.', item.extension_name | to_string ] | join %}
{%- if is_linux_x86(item.target_platform) %}
{{ uniqueId }} = vscode-utils.extensionFromVscodeMarketplace {
name = "{{ item.extension_name }}";
publisher = "{{ item.publisher_name }}";
version = "{{ item.extension_version }}";
sha256 = "{{ item.sha256 }}";
arch = "linux-x64";
};
{%- endif %}
{%- endwith %}
{%- endif %}
{%- endfor %}
}//
lib.attrsets.optionalAttrs(isLinux && (isAarch32 || isAarch64) ){
{%- for item in extensions %}
{%- if item.asset_url == None %}
{%- with uniqueId = [ item.publisher_name | to_string, '.', item.extension_name | to_string ] | join %}
{%- if is_linux_arm(item.target_platform) %}
{{ uniqueId }} = vscode-utils.extensionFromVscodeMarketplace {
name = "{{ item.extension_name }}";
publisher = "{{ item.publisher_name }}";
version = "{{ item.extension_version }}";
sha256 = "{{ item.sha256 }}";
arch = "linux-arm64";
};
{%- endif %}
{%- endwith %}
{%- endif %}
{%- endfor %}
}//
lib.attrsets.optionalAttrs(isDarwin && (isi686 || isx86_64) ){
{%- for item in extensions %}
{%- if item.asset_url == None %}
{%- with uniqueId = [ item.publisher_name | to_string, '.', item.extension_name | to_string ] | join %}
{%- if is_darwin_x86(item.target_platform) %}
{{ uniqueId }} = vscode-utils.extensionFromVscodeMarketplace {
name = "{{ item.extension_name }}";
publisher = "{{ item.publisher_name }}";
version = "{{ item.extension_version }}";
sha256 = "{{ item.sha256 }}";
arch = "darwin-x64";
};
{%- endif %}
{%- endwith %}
{%- endif %}
{%- endfor %}
}//
lib.attrsets.optionalAttrs(isDarwin && (isAarch32 || isAarch64) ){
{%- for item in extensions %}
{%- if item.asset_url == None %}
{%- with uniqueId = [ item.publisher_name | to_string, '.', item.extension_name | to_string ] | join %}
{%- if is_darwin_arm(item.target_platform) %}
{{ uniqueId }} = vscode-utils.extensionFromVscodeMarketplace {
name = "{{ item.extension_name }}";
publisher = "{{ item.publisher_name }}";
version = "{{ item.extension_version }}";
sha256 = "{{ item.sha256 }}";
arch = "darwin-arm64";
};
{%- endif %}
{%- endwith %}
{%- endif %}
{%- endfor %}
Expand Down
Loading

0 comments on commit dd1c636

Please sign in to comment.