Exemplo n.º 1
0
        private void processTaskScript(String scriptProcessingLocation, Task task, TaskScript taskScript)
        {
            _log.Debug("Determining task type");

            IProcessor processor = null;

            switch (taskScript.Type)
            {
            case ScriptType.Git:
                processor = new GitProcessor(scriptProcessingLocation, taskScript);
                break;

            case ScriptType.CommandLine:
                processor = new CommandLineProcessor(scriptProcessingLocation, _nodeLocation, taskScript);
                break;

            case ScriptType.Node:
                processor = new NodeProcessor(scriptProcessingLocation, _nodeLocation, taskScript);
                break;

            case ScriptType.Grunt:
                processor = new GruntProcessor(scriptProcessingLocation, _nodeLocation, taskScript);
                break;

            case ScriptType.PhantomJS:
                processor = new PhantomJSProcessor(scriptProcessingLocation, _phantomJSLocation, taskScript);
                break;
            }

            String processResults = String.Empty;

            if (processor != null)
            {
                _log.Debug("Starting to process task");

                if (!Directory.Exists(scriptProcessingLocation))
                {
                    Directory.CreateDirectory(scriptProcessingLocation);
                }

                _log.Debug("Checking for any required node packages");

                foreach (String requiredPackage in processor.GetRequiredPackages())
                {
                    PackageCache packageCache = _packageCacheDataAccess.Get(task.Project.ID, requiredPackage);

                    _log.Debug("Package required: " + requiredPackage);

                    String packageCacheLocation  = _packageCacheLocation.TrimEnd(new char[] { '\\' }) + "\\" + task.Project.ID + "\\" + requiredPackage;
                    String targetPackageLocation = scriptProcessingLocation.TrimEnd(new char[] { '\\' }) + "\\node_modules\\" + requiredPackage;

                    _log.Debug("Package cache location: " + packageCacheLocation);
                    _log.Debug("target package location: " + targetPackageLocation);

                    if ((packageCache == null) ||
                        ((packageCache != null) && (!packageCache.Store)) ||
                        ((packageCache != null) && (packageCache.Store) && (!Directory.Exists(packageCacheLocation))))
                    {
                        _log.Debug("Cached package does not exist so importing");

                        processResults += CommandWindowHelper.ProcessCommand(scriptProcessingLocation, _nodeLocation, 5, "npm install " + requiredPackage) + Environment.NewLine;
                    }
                    else
                    {
                        _log.Debug("Cached package exists so attempting to pull into place");

                        Boolean packageImportSuccessful = IOHelper.CopyDirectory(packageCacheLocation, targetPackageLocation);

                        _log.Debug("Check if the cach package copy was successfull");

                        if (!packageImportSuccessful)
                        {
                            _log.Debug("Cache package copy failed so pulling " + requiredPackage + " from npm");

                            processResults += CommandWindowHelper.ProcessCommand(scriptProcessingLocation, _nodeLocation, 5, "npm install " + requiredPackage) + Environment.NewLine;
                        }
                    }

                    _log.Debug("Checking that we now have the target package");

                    if (Directory.Exists(targetPackageLocation))
                    {
                        _log.Debug("We do have the package so copy it into cache");

                        Boolean copySuccessful = true;

                        if ((packageCache == null) ||
                            ((packageCache != null) && (packageCache.Store) && (!Directory.Exists(packageCacheLocation))))
                        {
                            copySuccessful = IOHelper.CopyDirectory(targetPackageLocation, packageCacheLocation);
                        }

                        if ((packageCache == null) && (copySuccessful))
                        {
                            packageCache            = new PackageCache();
                            packageCache.ExternalId = System.Guid.NewGuid().ToString();
                            packageCache.Name       = requiredPackage;
                            packageCache.Version    = IOHelper.GetPackageVersion(targetPackageLocation);
                            packageCache.Store      = true;
                            packageCache.Project    = task.Project;

                            packageCache = _packageCacheDataAccess.Insert(packageCache);
                        }
                    }
                }

                processResults += processor.Process();
            }

            _taskScriptDataAccess.UpdateTaskScriptLog(taskScript.ID, processResults);
        }