| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- using Invercargill;
- namespace Usm {
- public class CachedPackage {
- public string state_path { get; private set; }
- public string package_path { get; private set; }
- public string package_name { get; private set; }
- /**
- * The package's manifest when already known without reading the
- * cached .usmc — set from a repository listing entry so a plan
- * can be described, confirmed and strategised before the package
- * is downloaded. Null (the default) reads {@link package_path}
- * as usual; execution itself always works from the extracted
- * sources' own MANIFEST.usm.
- */
- public Manifest? known_manifest { get; set; }
- public CachedPackage(string path) {
- this.state_path = path;
- this.package_path = Path.build_filename(path, "package.usmc");
- this.package_name = Path.get_basename(path);
- }
- public Manifest get_manifest() throws Error {
- if(known_manifest != null) {
- return known_manifest;
- }
- return new Manifest.from_package(package_path);
- }
- public OriginInformation get_origin_information() throws Error {
- return new OriginInformation.from_file(Path.build_filename(state_path, "origin-info"));
- }
- public bool has_build_directory() {
- return File.new_for_path(build_directory_path()).query_exists();
- }
- public bool has_build_archive() {
- return File.new_for_path(build_archive_path()).query_exists();
- }
- public string create_build_directory() throws Error {
- var path = build_directory_path();
- var manifest = get_manifest();
- var file = File.new_for_path(path);
- if(manifest.flags.has(ManifestFlag.BUILD_IN_SOURCE_TREE)) {
- file.make_symbolic_link(source_directory_path());
- }
- else {
- File.new_for_path(path).make_directory();
- }
- return path;
- }
- public string get_build_directory() throws Error {
- var path = build_directory_path();
- if(File.new_for_path(path).query_exists()) {
- return path;
- }
- var archive_path = build_archive_path();
- if(!File.new_for_path(archive_path).query_exists()) {
- throw new StateError.NO_BUILD_ARTIFACT(@"The package \"$package_name\" has no build artifact");
- }
- Util.unarchive(archive_path, path);
- return path;
- }
- public string get_build_archive() throws Error {
- var path = build_archive_path();
- if(File.new_for_path(path).query_exists()) {
- return path;
- }
- var dir_path = build_directory_path();
- if(!File.new_for_path(dir_path).query_exists()) {
- throw new StateError.NO_BUILD_ARTIFACT(@"The package \"$package_name\" has no build artifact");
- }
- Util.archive(dir_path, path);
- return path;
- }
- public void archive_build(Usm.ProgressDelegate? progress = null) throws Error {
- // An archive with no build directory is already the
- // authoritative artifact: extracting it just to repack the
- // same bytes is pure churn
- if(!has_build_directory() && has_build_archive()) {
- return;
- }
- var build_dir = get_build_directory();
- // Delete existing archive, if it exists
- var archive_file = File.new_for_path(build_archive_path());
- if(archive_file.query_exists()) {
- archive_file.delete();
- }
- Util.archive_with_progress(build_dir, archive_file.get_path(), progress);
- clean_build_directory();
- }
- public void unarchive_build() throws Error {
- clean_build_directory();
- get_build_directory();
- }
- public void clean_build_directory() throws Error {
- var path = build_directory_path();
- if(File.new_for_path(path).query_exists()) {
- Util.delete_tree(path);
- }
- }
- /**
- * Removes the build directory AND the build.tar.xz archive,
- * forcing the next build to compile from scratch.
- */
- public void clean_build_cache() throws Error {
- clean_build_directory();
- var archive = build_archive_path();
- var archive_file = File.new_for_path(archive);
- if(archive_file.query_exists()) {
- archive_file.delete();
- }
- }
- /**
- * Extracts the cached .usmc into the package directory,
- * forwarding extraction progress (see
- * {@link Util.unarchive_with_progress}) to {@link progress}
- * when supplied. A directory that already exists is returned
- * as-is without extracting.
- */
- public string get_source_directory(Usm.ProgressDelegate? progress = null) throws Error {
- var path = source_directory_path();
- if(File.new_for_path(path).query_exists()) {
- return path;
- }
- Util.unarchive_with_progress(package_path, path, progress);
- return path;
- }
- public void clean_source() throws Error {
- var path = source_directory_path();
- if(File.new_for_path(path).query_exists()) {
- Util.delete_tree(path);
- }
- }
- public string create_install_directory() throws Error {
- var path = install_directory_path();
- File.new_for_path(path).make_directory();
- return path;
- }
- public void clean_install() throws Error {
- var path = install_directory_path();
- if(File.new_for_path(path).query_exists()) {
- Util.delete_tree(path);
- }
- }
- public void update_origin_information(OriginInformation info) throws Error {
- var properties = OriginInformation.get_mapper().map_from(info);
- var json = new InvercargillJson.JsonElement.from_properties(properties);
- json.write_to_file(Path.build_filename(state_path, "origin-info"));
- }
- private string build_directory_path() {
- return Path.build_filename(state_path, "build");
- }
- private string build_archive_path() {
- return Path.build_filename(state_path, "build.tar.xz");
- }
- private string source_directory_path() {
- return Path.build_filename(state_path, "package");
- }
- private string install_directory_path() {
- return Path.build_filename(state_path, "install");
- }
- }
- public class CachedPackageManifest {
- public CachedPackage package { get; private set; }
- public Manifest manifest { get; private set; }
- public CachedPackageManifest(CachedPackage package) throws Error {
- this.package = package;
- this.manifest = package.get_manifest();
- }
- }
- }
|