Esempio n. 1
0
        /// <inheritdoc />
        public string Archive(string sources, string target, string[] excludes = null, bool ignoreFilters = false)
        {
            sources = BaseFileSystem.GetNormalizePath(sources);

            using (var savedFile = fileSystem.Create(target))
                using (var zipArchive = new ZipArchive(savedFile, ZipArchiveMode.Create))
                {
                    var finder = new ArchivableFilesFinder(sources, excludes, ignoreFilters, fileSystem);
                    foreach (var file in finder)
                    {
                        if (file.EndsWith("/", StringComparison.Ordinal))
                        {
                            zipArchive.CreateEntry(file);
                            continue;
                        }

                        var entry = zipArchive.CreateEntry(file, CompressionLevel);
                        using (var entryStream = entry.Open())
                            using (var archiveFile = fileSystem.Read(Path.Combine(sources, file)))
                            {
                                archiveFile.CopyTo(entryStream);
                            }
                    }
                }

            return(target);
        }
Esempio n. 2
0
        /// <summary>
        /// Copy a local file into the cache.
        /// </summary>
        /// <param name="cache">The cache system instance.</param>
        /// <param name="file">The cache file name.</param>
        /// <param name="source">The source file absolute path.</param>
        /// <returns>True if the copy successful.</returns>
        public static bool CopyFrom(this ICache cache, string file, string source)
        {
            if (!cache.Enable)
            {
                return(false);
            }

            var local = new FileSystemLocal();

            using (var stream = local.Read(source))
            {
                cache.Write(file, stream);
            }

            return(true);
        }
Esempio n. 3
0
 public void TestUnauthorizedRead()
 {
     fileSystem.Read("../foo");
 }
Esempio n. 4
0
        /// <summary>
        /// Creates a <see cref="Bucket"/> object from specified <paramref name="cwd"/>.
        /// </summary>
        /// <remarks>If no <paramref name="cwd"/> is given, the current environment path will be used.</remarks>
        /// <param name="io">The input/output instance.</param>
        /// <param name="localBucket">either a configuration filename to read from, if null it will read from the default filename.</param>
        /// <param name="disablePlugins">Whether plugins should not be loaded.</param>
        /// <param name="cwd">Current working directory.</param>
        /// <param name="fullLoad">Whether to initialize everything or only main project stuff (used when loading the global bucket and auth).</param>
        /// <exception cref="InvalidArgumentException">If local config file not exists.</exception>
        public Bucket CreateBucket(IIO io, object localBucket = null, bool disablePlugins = false, string cwd = null, bool fullLoad = true)
        {
            cwd = cwd ?? Environment.CurrentDirectory;
            var    localFileSystem = new FileSystemLocal(cwd);
            var    config          = CreateConfig(io, cwd);
            string bucketFile      = null;

            if (localBucket == null || localBucket is string)
            {
                bucketFile = localBucket?.ToString();
                bucketFile = string.IsNullOrEmpty(bucketFile) ? GetBucketFile() : bucketFile;

                var localBucketFile = new JsonFile(bucketFile, localFileSystem, io);
                if (!localBucketFile.Exists())
                {
                    string message;
                    if (bucketFile == "./bucket.json" || bucketFile == "bucket.json")
                    {
                        message = $"Bucket could not find a bucket.json file in {cwd}";
                    }
                    else
                    {
                        message = $"Bucket could not find the config file {bucketFile} in {cwd}";
                    }

                    var instructions = "To initialize a project, please create a bucket.json";
                    throw new InvalidArgumentException(message + Environment.NewLine + instructions);
                }

                localBucketFile.Validate(false);

                io.WriteError($"Loading bucket file: {localBucketFile.GetPath()}", true, Verbosities.Debug);
                config.Merge(localBucketFile.Read());
                config.SetSourceBucket(new JsonConfigSource(localBucketFile));

                var localAuthFile = new JsonFile("./auth.json", localFileSystem, io);
                if (localAuthFile.Exists())
                {
                    io.WriteError($"Loading bucket file: {localAuthFile.GetPath()}", true, Verbosities.Debug);
                    config.Merge(new JObject
                    {
                        ["config"] = localAuthFile.Read(),
                    });
                    config.SetSourceAuth(new JsonConfigSource(localAuthFile, true));
                }

                localBucket = localBucketFile.Read <ConfigBucket>();
            }
            else if (localBucket is ConfigBucket configBucket)
            {
                config.Merge(JObject.FromObject(configBucket));
            }
            else
            {
                throw new UnexpectedException($"Params \"{localBucket}\" only allow {nameof(ConfigBucket)} type or string path.");
            }

            if (fullLoad)
            {
                new LoaderIOConfiguration(io).Load(config);
            }

            // initialize bucket.
            var bucket = new Bucket();

            bucket.SetConfig(config);

            var transport = CreateTransport(io, config);

            // initialize event dispatcher.
            var dispatcher = new BEventDispatcher(bucket, io);

            bucket.SetEventDispatcher(dispatcher);

            // initialize repository manager
            var repositoryManager = RepositoryFactory.CreateManager(io, config, dispatcher);

            bucket.SetRepositoryManager(repositoryManager);

            InitializeLocalInstalledRepository(io, repositoryManager, config.Get(Settings.VendorDir));

            // load root package
            var versionParser = new BVersionParser();
            var loader        = new LoaderPackageRoot(repositoryManager, config, io, versionParser);
            var package       = loader.Load <IPackageRoot>((ConfigBucket)localBucket);

            bucket.SetPackage(package);

            // create installation manager
            var installationManager = CreateInstallationManager();

            bucket.SetInstallationManager(installationManager);

            if (fullLoad)
            {
                var downloadManager = DownloadFactory.CreateManager(io, config, transport, dispatcher);
                bucket.SetDownloadManager(downloadManager);

                // todo: initialize archive manager
            }

            // must happen after downloadManager is created since they read it out of bucket.
            InitializeDefaultInstallers(installationManager, bucket, io);

            if (fullLoad)
            {
                Bucket globalBucket = null;
                if (BaseFileSystem.GetNormalizePath(cwd) != BaseFileSystem.GetNormalizePath(config.Get(Settings.Home)))
                {
                    globalBucket = CreateGlobalBucket(io, config, disablePlugins, false);
                }

                var pluginManager = new PluginManager(io, bucket, globalBucket, disablePlugins);
                bucket.SetPluginManager(pluginManager);
                pluginManager.LoadInstalledPlugins();
            }

            // init locker if locker exists.
            if (fullLoad && !string.IsNullOrEmpty(bucketFile))
            {
                var lockFile = Path.GetExtension(bucketFile) == ".json" ?
                               bucketFile.Substring(0, bucketFile.Length - 5) + ".lock" :
                               bucketFile + ".lock";
                var locker = new Locker(
                    io,
                    new JsonFile(lockFile, localFileSystem, io), installationManager, localFileSystem.Read(bucketFile).ToText());
                bucket.SetLocker(locker);
            }

            if (fullLoad)
            {
                // Raises an event only when it is fully loaded.
                dispatcher.Dispatch(PluginEvents.Init, this);
            }

            if (fullLoad)
            {
                // once everything is initialized we can purge packages from
                // local repos if they have been deleted on the filesystem.
                PurgePackages(repositoryManager.GetLocalInstalledRepository(), installationManager);
            }

            return(bucket);
        }