Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to package shipped apps #451

Merged
merged 1 commit into from
Mar 14, 2019
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Usage:
krankerl list apps <version>
krankerl list categories
krankerl login (--appstore | --github) <token>
krankerl package
krankerl package [--shipped]
krankerl publish [--nightly] <url>
krankerl sign --package
krankerl up
Expand Down
4 changes: 2 additions & 2 deletions src/commands/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ use failure::Error;

use packaging::package_app as package;

pub fn package_app(app_path: &PathBuf) -> Result<(), Error> {
package(app_path)
pub fn package_app(app_path: &PathBuf, shipped: bool) -> Result<(), Error> {
package(app_path, shipped)
}
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Usage:
krankerl list apps <version>
krankerl list categories
krankerl login (--appstore | --github) <token>
krankerl package
krankerl package [--shipped]
krankerl publish [--nightly] <url>
krankerl sign --package
krankerl up
Expand Down Expand Up @@ -58,6 +58,7 @@ struct Args {
flag_github: bool,
flag_nightly: bool,
flag_package: bool,
flag_shipped: bool,
flag_version: bool,
}

Expand Down Expand Up @@ -122,7 +123,7 @@ fn main() {
krankerl::commands::log_in_to_github(&token).expect("could not save github token");
}
} else if args.cmd_package {
krankerl::commands::package_app(&PathBuf::from("."))
krankerl::commands::package_app(&PathBuf::from("."), args.flag_shipped)
.unwrap_or_else(|e| println!("could not package app: {}", e));
} else if args.cmd_publish {
let url = args.arg_url.unwrap();
Expand Down
59 changes: 50 additions & 9 deletions src/packaging/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::path::PathBuf;
use std::thread;

use failure::Error;
use indicatif::MultiProgress;
use indicatif::{MultiProgress, ProgressBar};

mod archive;
mod artifacts;
Expand All @@ -11,8 +11,39 @@ mod exclude;
mod pipeline;

use console::default_spinner;
use packaging::pipeline::App;

pub fn package_app(app_path: &PathBuf) -> Result<(), Error> {
fn build_archive(
app_path: PathBuf,
prog_clone: ProgressBar,
prog_dependencies: ProgressBar,
prog_build: ProgressBar,
prog_package: ProgressBar,
) -> Result<(), Error> {
App::new(app_path)
.clone(Some(prog_clone))?
.install_dependencies(Some(prog_dependencies))?
.build(Some(prog_build))?
.into_archive(Some(prog_package))?;
Ok(())
}

fn build_shipped(
app_path: PathBuf,
prog_clone: ProgressBar,
prog_dependencies: ProgressBar,
prog_build: ProgressBar,
prog_package: ProgressBar,
) -> Result<(), Error> {
App::new(app_path)
.clone(Some(prog_clone))?
.install_dependencies(Some(prog_dependencies))?
.build(Some(prog_build))?
.into_shipped(Some(prog_package))?;
Ok(())
}

pub fn package_app(app_path: &PathBuf, shipped: bool) -> Result<(), Error> {
let app_path = app_path.clone();
let mp = MultiProgress::new();
let prog_clone = mp.add(default_spinner());
Expand All @@ -29,13 +60,23 @@ pub fn package_app(app_path: &PathBuf) -> Result<(), Error> {
prog_package.set_message("waiting...");

let worker = thread::spawn(move || {
use self::pipeline::App;

App::new(app_path)
.clone(Some(prog_clone))?
.install_dependencies(Some(prog_dependencies))?
.build(Some(prog_package_cmds))?
.into_archive(Some(prog_package))
if shipped {
build_shipped(
app_path,
prog_clone,
prog_dependencies,
prog_package_cmds,
prog_package,
)
} else {
build_archive(
app_path,
prog_clone,
prog_dependencies,
prog_package_cmds,
prog_package,
)
}
});

mp.join()?;
Expand Down
69 changes: 68 additions & 1 deletion src/packaging/pipeline.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::fs::File;
use std::fs::{copy, create_dir_all, File};
use std::path::{Path, PathBuf};

use failure::Error;
use flate2::write::GzEncoder;
use flate2::Compression;
use indicatif::ProgressBar;
use nextcloud_appinfo::{get_appinfo, AppInfo};
use pathdiff::diff_paths;
use tempdir::TempDir;
use walkdir::{DirEntry, WalkDir};

Expand Down Expand Up @@ -170,6 +171,40 @@ impl BuiltApp {

Ok(AppArchive::new(self))
}

pub fn into_shipped(self, progress: Option<ProgressBar>) -> Result<ShippedApp, Error> {
let mut ship_path = self.app.source_path.to_path_buf();
ship_path.push("build");
ship_path.push("artifacts");
artifacts::clear(&ship_path)?;
ship_path.push(self.app_info.id());
progress
.as_ref()
.map(|prog| prog.set_message(&format!("Writing app files to {:?}...", ship_path)));

let app_path = tmp_app_path(self.tmp_dir.path(), self.app_info.id());
{
let excludes = exclude::ExcludedFiles::new(self.config.package().exclude())?;
for entry in build_file_list(&app_path, &excludes) {
if !entry.metadata().unwrap().is_dir() {
let entry_path = entry.path();
if let Some(normalized) = diff_paths(&entry_path, &app_path) {
let mut file_path = ship_path.clone();
file_path.push(&normalized);
if let Some(parent) = file_path.parent() {
create_dir_all(parent)?;
}
copy(&entry.path(), &file_path)?;
}
}
}
}

progress.as_ref().map(|prog| {
prog.finish_with_message(&format!("App directory created at {:?}", ship_path))
});
Ok(ShippedApp::new(self))
}
}

fn build_file_list(build_path: &Path, excludes: &exclude::ExcludedFiles) -> Vec<DirEntry> {
Expand All @@ -188,6 +223,14 @@ impl AppArchive {
}
}

pub struct ShippedApp {}

impl ShippedApp {
pub fn new(_app: BuiltApp) -> Self {
ShippedApp {}
}
}

#[cfg(test)]
mod tests {
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -280,4 +323,28 @@ mod tests {
assert!(final_path.exists(), "app archive does not exist");
}

#[test]
fn create_shipped_app_directory() {
let dir = create_test_app_dir(MINIMALIST_APP);
let app = App::new(get_test_app_path(dir.path()));
let clone = app.clone(None).unwrap();
let installed = clone.install_dependencies(None).unwrap();
let built = installed.build(None).unwrap();

built.into_shipped(None).unwrap();

let mut app_info_path = dir.path().to_path_buf();
app_info_path.push(APP_ID);
app_info_path.push("appinfo");
app_info_path.push("info.xml");
assert!(app_info_path.exists(), "info.xml is missing");
let mut package_json_path = dir.path().to_path_buf();
package_json_path.push(APP_ID);
package_json_path.push("package.json");
assert!(
!package_json_path.exists(),
"package.json should not be copied"
);
}

}