8000 refactor code generation by figsoda · Pull Request #841 · nix-community/nix-init · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 0 additions & 73 deletions src/builder.rs

This file was deleted.

58 changes: 58 additions & 0 deletions src/codegen/drv.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::fmt::{self, Display, Formatter};

use crate::{cli::CargoVendor, codegen::Builder};

#[derive(Clone, Copy)]
pub struct MkDerivation {
cc: bool,
rust: Option<CargoVendor>,
}

impl MkDerivation {
pub fn new(rust: Option<CargoVendor>) -> Self {
Self { cc: true, rust }
}

pub fn no_cc() -> Self {
Self {
cc: false,
rust: None,
}
}
}

impl Builder for MkDerivation {
fn function(&self) -> &'static str {
if self.cc {
"stdenv.mkDerivation"
} else {
"stdenvNoCC.mkDerivation"
}
}

fn explicit_strict_deps(&self) -> bool {
true
}

fn cargo_deps(&self) -> Option<CargoVendor> {
self.rust
}

fn infer_setup_hooks(&self) -> bool {
true
}

fn explicit_platforms(&self) -> bool {
true
}
}

impl Display for MkDerivation {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.function())?;
if let Some(rust) = self.rust {
write!(f, " + {rust}")?;
}
Ok(())
}
}
56 changes: 56 additions & 0 deletions src/codegen/go.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::fmt::Write as _;

use anyhow::Result;
use parse_display::Display;
use tracing::warn;

use crate::{
codegen::{Builder, Codegen},
lang::go::{load_go_dependencies, write_ldflags},
utils::{FAKE_HASH, ResultExt, fod_hash},
};

#[derive(Clone, Copy, Display)]
#[display("buildGoModule")]
pub struct BuildGoModule;

impl Builder for BuildGoModule {
fn function(&self) -> &'static str {
"buildGoModule"
}

async fn after_src(&self, cg: &mut Codegen<'_>) -> Result<String> {
let mut out = String::new();
let go_sum = std::fs::File::open(cg.src_dir.join("go.sum")).ok_inspect(|e| warn!("{e}"));

if let Some(go_sum) = &go_sum {
load_go_dependencies(&mut cg.inputs, go_sum);
}

let hash = if cg.src_dir.join("vendor").is_dir()
|| go_sum
.and_then(|go_sum| go_sum.metadata().ok_inspect(|e| warn!("{e}")))
.is_none_or(|metadata| metadata.len() == 0)
{
"null".into()
} else if let Some(hash) = fod_hash(format!(
r#"(import({}){{}}).buildGoModule{{pname={:?};version={:?};src={};vendorHash="{FAKE_HASH}";}}"#,
cg.nixpkgs, cg.pname, cg.version, cg.src,
))
.await
{
format!(r#""{hash}""#)
} else {
format!(r#""{FAKE_HASH}""#)
};

writeln!(out, " vendorHash = {hash};\n")?;
Ok(out)
}

fn after_inputs(&self, cg: &mut Codegen<'_>) -> Result<String> {
let mut out = String::new();
write_ldflags(&mut out, cg.src_dir)?;
Ok(out)
}
}
Loading
0