Skip to content

Commit

Permalink
Use askama templates in cargo swift package command
Browse files Browse the repository at this point in the history
  • Loading branch information
antoniusnaumann committed Oct 13, 2023
1 parent 3e4c34d commit 689ead6
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 48 deletions.
17 changes: 10 additions & 7 deletions src/commands/init.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use askama::Template;
use std::fmt::Display;
use std::fs::{create_dir, write};
use std::process::Stdio;
use askama::Template;

use clap::ValueEnum;
use execute::{command, Execute};
Expand Down Expand Up @@ -55,14 +55,17 @@ fn create_project(crate_name: &str, lib_type: LibType, plain: bool) -> Result<()
lib_type: lib_type.identifier(),
};
let lib_rs_content = templating::LibRs { plain };
let udl_content = templating::LibUdl { namespace: &namespace, plain };
let build_rs_content = templating::BuildRs { plain };
let udl_content = templating::LibUdl {
namespace: &namespace,
plain,
};
let build_rs_content = templating::BuildRs {};

write_project_files(
&cargo_toml_content.render()?,
&build_rs_content.render()?,
&lib_rs_content.render()?,
&udl_content.render()?,
&cargo_toml_content.render().unwrap(),
&build_rs_content.render().unwrap(),
&lib_rs_content.render().unwrap(),
&udl_content.render().unwrap(),
crate_name,
)?;

Expand Down
18 changes: 12 additions & 6 deletions src/swiftpackage.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
use askama::Template;
use std::fs::{copy, create_dir_all, write};

use crate::{recreate_dir, Result};
use crate::{recreate_dir, templating, Result};

/// Create artifacts for a swift package given the package name
///
/// **Note**: This method assumes that a directory with the package name and the .xcframework already exists
pub fn create_swiftpackage(package_name: &str, namespace: &str) -> Result<()> {
// TODO: Instead of assuming the directory and the xcframework, let this manage directory
// recreation and let it copy the xcframework
let package_manifest =
include_str!("../template/template.Package.swift").replace("<PACKAGE_NAME>", package_name);

write(format!("{}/Package.swift", package_name), package_manifest)
.map_err(|e| format!("Could not write Package.swift: \n {e}"))?;
let package_manifest = templating::PackageSwift {
package_name,
enable_warnings: false,
};

write(
format!("{}/Package.swift", package_name),
package_manifest.render().unwrap(),
)
.map_err(|e| format!("Could not write Package.swift: \n {e}"))?;

create_dir_all(format!("{}/Sources/{}", package_name, package_name))
.map_err(|e| format!("Could not create module sources directory: \n {e}"))?;
Expand Down
9 changes: 3 additions & 6 deletions src/templating.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use askama::Template;

use crate::LibType;

#[derive(Template)]
#[template(path = "Cargo.toml", escape = "none")]
pub(crate) struct CargoToml<'a> {
Expand All @@ -13,9 +11,7 @@ pub(crate) struct CargoToml<'a> {

#[derive(Template)]
#[template(path = "build.rs", escape = "none")]
pub(crate) struct BuildRs {
pub(crate) plain: bool,
}
pub(crate) struct BuildRs {}

#[derive(Template)]
#[template(path = "lib.rs", escape = "none")]
Expand All @@ -35,4 +31,5 @@ pub(crate) struct LibUdl<'a> {
pub(crate) struct PackageSwift<'a> {
pub(crate) package_name: &'a str,
pub(crate) enable_warnings: bool,
}
}

29 changes: 0 additions & 29 deletions template/template.Package.swift

This file was deleted.

0 comments on commit 689ead6

Please sign in to comment.