Beispiel #1
0
 public void TestValidatorInvalidPackage(string file, string expected)
 {
     var(warnings, publishErrors, errors) = validator.Validate(file);
     Assert.AreEqual(1, errors.Length);
     StringAssert.Contains(errors[0], expected);
 }
Beispiel #2
0
        /// <inheritdoc />
        protected override int Execute(IInput input, IOutput output)
        {
            var file       = input.GetArgument("file") ?? Factory.GetBucketFile();
            var io         = GetIO();
            var fileSystem = new FileSystemLocal();

            if (!fileSystem.Exists(file))
            {
                io.WriteError($"<error>{file} not found.</error>");
                return(ExitCodes.FileNotFoundException);
            }

            var checkPublish = !input.GetOption("no-check-publish");
            var checkLock    = !input.GetOption("no-check-lock");
            var isStrict     = input.GetOption("strict");

            var validator = new ValidatorBucket(fileSystem, io);

            var(warnings, publishErrors, errors) = validator.Validate(file);

            var lockErrors = Array.Empty <string>();
            var bucket     = Factory.Create(io, file, input.HasRawOption("--no-plugins"));
            var locker     = bucket.GetLocker();

            if (locker.IsLocked() && !locker.IsFresh())
            {
                lockErrors = new[] { "The lock file is not up to date with the latest changes in bucket.json, it is recommended that you run `bucket update`." };
            }

            OuputResult(file, ref errors, ref warnings, checkPublish, publishErrors, checkLock, lockErrors, isStrict);

            int GetStrictExitCode()
            {
                return((isStrict && !warnings.Empty()) ? ExitCodes.ValidationWarning : GameBox.Console.ExitCodes.Normal);
            }

            var exitCode = errors.Length > 0 ? ExitCodes.ValidationErrors : GetStrictExitCode();

            if (input.GetOption("with-dependencies"))
            {
                var localInstalledRepository = bucket.GetRepositoryManager().GetLocalInstalledRepository();
                foreach (var package in localInstalledRepository.GetPackages())
                {
                    var path = bucket.GetInstallationManager().GetInstalledPath(package);
                    file = Path.Combine(path, "bucket.json");

                    if (!Directory.Exists(path) || !File.Exists(file))
                    {
                        continue;
                    }

                    (warnings, publishErrors, errors) = validator.Validate(file);
                    OuputResult(file, ref errors, ref warnings, checkPublish, publishErrors, isStrict: isStrict);

                    var depCode = !errors.Empty() ? ExitCodes.ValidationErrors : GetStrictExitCode();
                    exitCode = Math.Max(depCode, exitCode);
                }
            }

            var commandEvent = new CommandEventArgs(PluginEvents.Command, "validate", input, output);

            bucket.GetEventDispatcher().Dispatch(this, commandEvent);

            return(Math.Max(exitCode, commandEvent.ExitCode));
        }