Example #1
0
        public void Compile(DroneEnv env, LogLevel logLevel)
        {
            if (this.IsRecompileNeeded(env.Config))
            {
                var result = this.CompileCore(env);

                try
                {
                    if (result.IsSuccess)
                    {
                        this.CreateCache(env.Config);
                        this.log.Log(logLevel, "compiled ({0})", HumanTime.Format(result.TimeElapsed));
                    }
                    else
                    {
                        throw DroneCompilerException.Get(result);
                    }
                }
                finally
                {
                    this.LogResult(result);
                }
            }
            else
            {
                this.log.Log(logLevel, "compiliation skipped, all files up to date");
            }
        }
Example #2
0
        public IEnumerable<DroneTask> GetTasks(DroneEnv env)
        {
            DroneEnv.Set(env);
            var module = this.CompileAndLoadModule(env, LogLevel.Debug);

            foreach (var task in module.Tasks)
            {
                var taskCopy = new DroneTask(task.Name, task.Dependencies);
                yield return taskCopy;
            }
        }
Example #3
0
        /// <summary>
        /// Runs the tasks from the given module
        /// </summary>
        /// <param name="module">The module.</param>
        /// <param name="taskNames">The task names.</param>
        /// <param name="env">The env.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">module
        /// or
        /// taskNames
        /// or
        /// config</exception>
        public IList<DroneTaskResult> Run(DroneModule module, IEnumerable<string> taskNames, DroneEnv env)
        {
            if (module == null)
                throw new ArgumentNullException("module");

            if (taskNames == null)
                throw new ArgumentNullException("taskNames");

            if(env == null)
                throw new ArgumentNullException("env");

            var names = taskNames.ToList();
            var results = new List<DroneTaskResult>();
            var sw = new Stopwatch();
            sw.Start();

            if (names.Count == 0)
            {
                this.log.Debug("no task names provided, trying to run default task");

                var result = this.TryRunDefaultTask(module, env);

                if(result.HasValue)
                    results.Add(result.Value);
            }
            else
            {
                this.log.Debug("checking task names exist...");

                this.EnsureTaskNamesExists(module, names);

                this.log.Debug("all task names found");

                var taskFaulted = false;

                this.log.Debug("running tasks...");

                foreach(var taskName in names)
                {
                    this.log.Debug("finding task '{0}'", taskName);

                    var task = module.TryGet(taskName);

                    if(taskFaulted)
                    {
                        this.log.Debug("skipping task '{0}' due to previous task failure", taskName);
                        results.Add(new DroneTaskResult(task, DroneTaskState.NotRan, TimeSpan.Zero, Option.None<Exception>()));
                        continue;
                    }

                    this.log.Debug("task '{0}' found!, running...", taskName);

                    var result = this.Run(module, task.Value, env, true);

                    results.Add(result);

                    if (!result.IsSuccess)
                    {
                        this.log.Debug("task '{0}' has failed, skipping all other tasks", taskName);

                        taskFaulted = true;
                    }

                    this.log.Info(string.Empty);
                }

                this.log.Debug("all tasks completed");
            }

            if(results.Count == 0)
                return results;

            var maxNameLen = results.Max(x => x.Task.Get(t => t.Name.Length, 0));

            var totalTime = TimeSpan.FromMilliseconds(results.Select(x => x.TimeElapsed.TotalMilliseconds).Sum());

            var summaryTitle = string.Format("tasks: ({0})", HumanTime.Format(totalTime));

            this.log.Info(summaryTitle);
            this.log.Info(string.Join(string.Empty, Enumerable.Repeat("-", summaryTitle.Length)));

            foreach(var result in results)
            {
                var glyph = this.GetTaskStateGlyph(result.State);
                var state = this.GetTaskStateDesc(result.State);
                var name = result.Task.Get(x => x.Name, "[null]");
                var time = string.Format("({0})", this.GetTaskStateFormatedTime(result.State, result.TimeElapsed));
                var fmt = string.Format("{{0}} {{1, -{0}}} {{2, -10}} {{3, -10}}", maxNameLen);
                this.log.Info(fmt, glyph, name, state, time);
            }

            sw.Stop();
            this.log.Info(string.Empty);
            this.log.Info("total time: ({0})", HumanTime.Format(sw.Elapsed));

            return results;
        }
Example #4
0
        private Option<DroneTaskResult> TryRunDefaultTask(DroneModule module, DroneEnv env)
        {
            var task = module.TryGet(DroneModule.DefaultTaskName);

            if(task.HasValue)
            {
                this.log.Debug("default task found, running...");
                return Option.From(this.Run(module, task.Value, env, true));
            }
            else
            {
                this.log.Warn("no default task found");
                return Option.None<DroneTaskResult>();
            }
        }
Example #5
0
        private DroneTaskResult Run(DroneModule module, DroneTask task, DroneEnv env, bool logErrors)
        {
            var taskLog = DroneLogManager.GetTaskLog(task.Name);

            var taskContext = new DroneTaskContext(module, task, env, taskLog, (t, e) =>
            {
                var childTaskResult = this.Run(module, t, e, false);

                if(!childTaskResult.IsSuccess)
                {
                    this.log.Debug("child task '{0}' has failed", t.Name);
                    throw childTaskResult.Exception.Value;
                }
            });

            this.log.Info("running '{0}'", task.Name);

            var handler = this.factory.TryGetHandler(task);

            var result = FuncStopwatch.Run(() =>
            {
                if(handler.HasValue)
                {
                    handler.Value.Handle(taskContext);
                }
                else
                {
                    throw DroneTaskHandlerNotFoundException.Get(task);
                }
            });

            if (result.IsSuccess)
            {
                this.log.Info("task '{0}' completed ({1})", task.Name, HumanTime.Format(result.TimeElapsed));
                return new DroneTaskResult(Option.From(task), DroneTaskState.Completed, result.TimeElapsed, Option.None<Exception>());
            }
            else
            {
                var state = DroneTaskState.Faulted;
                var stateName = "failed";

                this.log.Info("task '{0}' {1} ({2})", task.Name, stateName, HumanTime.Format(result.TimeElapsed));

                if(logErrors)
                    this.log.ExceptionAndData(result.Exception);

                return new DroneTaskResult(Option.From(task), state, result.TimeElapsed, Option.From(result.Exception));
            }
        }
Example #6
0
        private CSharpCompilerResult CompileCore(DroneEnv env)
        {
            try
            {
                if (env == null)
                    throw new ArgumentNullException("env");

                var compiler = new CSharpCompiler();

                this.log.Debug("checking if output dir exists '{0}'", env.Config.BuildDirPath);

                if (!Directory.Exists(env.Config.BuildDirPath))
                {
                    this.log.Debug("output dir not found. creating output bin dir");
                    Directory.CreateDirectory(env.Config.BuildDirPath);
                }

                var configDirpath = Path.GetDirectoryName(env.Config.FilePath);

                var resolvedBaseReferences = this.GetBaseReferenceFiles(env.Config.DroneReferencesDirPath).ToList();
                var resolvedConfigReferences = this.ResolveReferenceFiles(env.Config.ReferenceFiles, configDirpath).ToList();

                var referenceFiles = resolvedBaseReferences.Concat(resolvedConfigReferences).Distinct().ToList();

                var sourceFiles = this.ResolveSourceFiles(env.Config.SourceFiles, configDirpath).ToList();

                this.log.Debug("creating csharp compiler args");

                var args = new CSharpCompilerArgs(
                    env.Config.DirPath,
                    env.Config.AssemblyFilePath,
                    sourceFiles,
                    referenceFiles);

                if (env.Flags.IsDebugEnabled)
                {
                    args.Debug = true;
                    args.Optimize = false;
                }
                else
                {
                    args.Debug = false;
                    args.Optimize = true;
                }

                if (this.log.IsDebugEnabled)
                {
                    this.log.Debug("csharp args");
                    this.log.Debug("work dir: '{0}'", args.WorkDir);
                    this.log.Debug("output filepath: '{0}'", args.OutputFilepath);
                    this.log.Debug("source files:");

                    foreach (var file in sourceFiles)
                        this.log.Debug(file);

                    this.log.Debug("reference files:");

                    foreach (var file in referenceFiles)
                        this.log.Debug(file);
                }

                this.log.Debug("calling csc compiler '{0}'...", env.Config.FileName);

                var result = compiler.Compile(args);

                this.log.Debug("csc result: {0}", result.IsSuccess);
                this.log.Debug("exit code: {0}", result.ExiteCode);

                if (result.IsSuccess)
                {
                    this.log.Debug("output assembly filepath: '{0}'", result.Success.OutputAssemblyFilepath);

                    var outputPath = Path.GetDirectoryName(result.Success.OutputAssemblyFilepath);

                    foreach (var refFile in resolvedConfigReferences)
                        File.Copy(refFile, Path.Combine(outputPath, Path.GetFileName(refFile)), true);
                }

                return result;
            }
            catch (Exception ex)
            {
                return CSharpCompilerResult.GetFailure(-1,
                    TimeSpan.Zero,
                    ex,
                    Enumerable.Empty<string>(),
                    Enumerable.Empty<string>(),
                    Enumerable.Empty<string>());
            }
        }
Example #7
0
 public void CompileTasks(DroneEnv env, LogLevel logLevel)
 {
     DroneEnv.Set(env);
     this.compiler.Compile(env, logLevel);
 }
Example #8
0
 private DroneModule CompileAndLoadModule(DroneEnv env, LogLevel logLevel)
 {
     this.compiler.Compile(env, logLevel);
     var module = this.loader.Load(env.Config);
     return module;
 }
Example #9
0
 public void RunTasks(DroneEnv env, IEnumerable<string> taskNames)
 {
     DroneEnv.Set(env);
     var module = this.CompileAndLoadModule(env, LogLevel.Debug);
     this.taskRunner.Run(module, taskNames, env);
 }