Esempio n. 1
0
        /// <inheritdoc />
        protected override void Initialize()
        {
            versionParser = new BVersionParser();
            AddPackage(CreatePluginApiPackage());

            foreach (var platform in platforms)
            {
                var version = versionParser.Normalize(platform.Value);
                var package = new PackageComplete(platform.Key, version, platform.Value);
                package.SetDescription("Package manually configured.");
                AddPackage(package);
            }
        }
Esempio n. 2
0
        /// <inheritdoc />
        public virtual void Update(IPackage initial, IPackage target, string cwd)
        {
            var name = target.GetName();
            var from = initial.GetVersionPrettyFull();
            var to   = target.GetVersionPrettyFull();

            var actionName = BVersionParser.IsUpgrade(initial.GetVersion(), target.GetVersion()) ? "Updating" : "Downgrading";

            io.WriteError($"  - {actionName} <info>{name}</info> (<comment>{from}</comment> => <comment>{to}</comment>): ", false);

            Remove(initial, cwd, false);
            Install(target, cwd, false);

            io.WriteError(string.Empty);
        }
Esempio n. 3
0
        /// <inheritdoc />
        public void Update(IPackage initial, IPackage target, string cwd)
        {
            GuardSourceReferenece(target);

            var    name = target.GetName();
            string from, to;

            if (initial.GetVersionPretty() == target.GetVersionPretty())
            {
                if (target.GetSourceType() == "svn")
                {
                    from = initial.GetSourceReference();
                    to   = target.GetSourceReference();
                }
                else
                {
                    // Git's reference, we only need to take the first 7 digits.
                    var fromReference = initial.GetSourceReference();
                    var toReference   = target.GetSourceReference();
                    from = fromReference.Length >= 7 ? fromReference.Substring(0, 7) : fromReference;
                    to   = toReference.Length >= 7 ? toReference.Substring(0, 7) : toReference;
                }

                name += $" {initial.GetVersionPretty()}";
            }
            else
            {
                from = initial.GetVersionPrettyFull();
                to   = target.GetVersionPrettyFull();
            }

            var actionName = BVersionParser.IsUpgrade(initial.GetVersion(), target.GetVersion()) ? "Updating" : "Downgrading";

            IO.WriteError($"  - {actionName} <info>{name}</info> (<comment>{from}</comment> => <comment>{to}</comment>): ", false);

            SException exception = null;

            try
            {
                CleanChanges(initial, cwd, true);

                ProcessAction(target.GetSourceUris(), (uri) =>
                {
                    DoUpdate(initial, target, cwd, uri);
                });
            }
#pragma warning disable CA1031
            catch (SException ex)
#pragma warning restore CA1031
            {
                exception = ex;
            }
            finally
            {
                ReapplyChanges(cwd);
            }

            // check metadata repository because in case of missing metadata
            // code would trigger another exception
            if (exception == null && IO.IsVerbose && HasMetadataRepository(cwd))
            {
                var message = "Pulling in changes:";
                var logs    = GetCommitLogs(initial.GetSourceReference(), target.GetSourceReference(), cwd);

                if (string.IsNullOrEmpty(logs))
                {
                    message = "Rolling back changes:";
                    logs    = GetCommitLogs(target.GetSourceReference(), initial.GetSourceReference(), cwd);
                }

                if (string.IsNullOrEmpty(logs))
                {
                    return;
                }

                logs = string.Join("\n", Arr.Map(logs.Split('\n'), line => $"      {line}"));

                // escape angle brackets for proper output in the console.
                logs = logs.Replace("<", @"\<");

                IO.WriteError($"    {message}");
                IO.WriteError(logs);
            }
            else if (exception != null)
            {
                ExceptionDispatchInfo.Capture(exception).Throw();
            }
        }
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);
        }