|
@@ -0,0 +1,84 @@
|
|
|
+using Invercargill;
|
|
|
+
|
|
|
+namespace Usm {
|
|
|
+
|
|
|
+ public class SystemState {
|
|
|
+
|
|
|
+ public string config_path { get; private set; }
|
|
|
+ public string state_path { get; private set; }
|
|
|
+
|
|
|
+ public SystemState(Paths paths) {
|
|
|
+ config_path = paths.usm_config_dir;
|
|
|
+ state_path = paths.usm_state_dir;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Enumerable<CachedPackage> get_cached_packages() throws Error {
|
|
|
+ return directory(Path.build_filename(state_path, "packages"))
|
|
|
+ .select<CachedPackage>(d => new CachedPackage(Path.build_filename(state_path, "packages", d)));
|
|
|
+ }
|
|
|
+
|
|
|
+ public Enumerable<CachedPackage> get_installed_packages() throws Error {
|
|
|
+ return directory(Path.build_filename(state_path, "installed"))
|
|
|
+ .select<CachedPackage>(d => new CachedPackage(Path.build_filename(state_path, "installed", d)));
|
|
|
+ }
|
|
|
+
|
|
|
+ public Enumerable<Repository> get_repositories() throws Error {
|
|
|
+ return directory(Path.build_filename(config_path, "repos.d"))
|
|
|
+ .try_select<Repository>(d => new Repository.from_file(Path.build_filename(state_path, "repos.d", d)))
|
|
|
+ .unwrap_to_series();
|
|
|
+ }
|
|
|
+
|
|
|
+ public RepositoryListing? get_latest_list(Repository repository) throws Error {
|
|
|
+ var list_path = Path.build_filename(state_path, "lists", repository.name);
|
|
|
+ if(!File.new_for_path(list_path).query_exists()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ var latest = directory(list_path)
|
|
|
+ .where(f => f.has_suffix(".usml"))
|
|
|
+ .contextualised_select<DateTime>(f => new DateTime.from_iso8601(f.replace(".usml", ""), null))
|
|
|
+ .sort((a, b) => a.result.compare(b.result))
|
|
|
+ .first_or_default();
|
|
|
+
|
|
|
+ var stream = new DataInputStream(File.new_build_filename(list_path, latest).read());
|
|
|
+ return new RepositoryListing.from_stream(stream);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void refresh_list(Repository repository, RepositoryClientProgressCallback? callback = null) throws Error {
|
|
|
+ var client = repository.get_client();
|
|
|
+
|
|
|
+ var list_path = Path.build_filename(state_path, "lists", repository.name);
|
|
|
+ if(!File.new_for_path(list_path).query_exists()) {
|
|
|
+ File.new_for_path(list_path).make_directory();
|
|
|
+ }
|
|
|
+
|
|
|
+ var filename = @"$(new DateTime.now_utc().format_iso8601()).usml";
|
|
|
+ client.download_repository_listing(Path.build_filename(list_path, filename), callback);
|
|
|
+ }
|
|
|
+
|
|
|
+ public string generate_cache_path(Manifest manifest) {
|
|
|
+ var filename = @"$(manifest.name)-$(manifest.version)";
|
|
|
+ return Path.build_filename(state_path, "packages", filename);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void mark_installed(CachedPackage package) throws Error {
|
|
|
+ var filename = Path.get_basename(package.state_path);
|
|
|
+ var symlink = File.new_build_filename(state_path, "installed", filename);
|
|
|
+ symlink.make_symbolic_link(package.state_path);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void unmark_installed(CachedPackage package) throws Error {
|
|
|
+ var filename = Path.get_basename(package.state_path);
|
|
|
+ var symlink = File.new_build_filename(state_path, "installed", filename);
|
|
|
+ symlink.delete();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public errordomain StateError {
|
|
|
+ NO_BUILD_ARTIFACT,
|
|
|
+ EXTRACTION_FAILED,
|
|
|
+ ARCHIVAL_FAILED,
|
|
|
+ }
|
|
|
+
|
|
|
+}
|