Пример #1
0
        public ServerArgs Parse(string jsonFilePath)
        {
            if (File.Exists(jsonFilePath))
            {
                ServerArgs fromFile = JsonConvert.DeserializeObject <ServerArgs>(File.ReadAllText(jsonFilePath));
                if (!String.IsNullOrWhiteSpace(fromFile.ModulePath))
                {
                    this.ModulePath = fromFile.ModulePath;
                }

                if (!String.IsNullOrWhiteSpace(fromFile.LogPipeName))
                {
                    this.LogPipeName = fromFile.LogPipeName;
                }

                if (fromFile.ExternalModules != null && fromFile.ExternalModules.Count > 0)
                {
                    this.ExternalModules = fromFile.ExternalModules;
                }

                if (fromFile.SpecificationPaths != null && fromFile.SpecificationPaths.Count > 0)
                {
                    this.SpecificationPaths = fromFile.SpecificationPaths;
                }

                if (fromFile.TransformDefinitionFiles != null && fromFile.TransformDefinitionFiles.Count > 0)
                {
                    // Let's merge these instead of overwriting maybe?
                    this.TransformDefinitionFiles.AddRange(fromFile.TransformDefinitionFiles);
                }
            }

            return(this);
        }
Пример #2
0
        static void Main(string[] args)
        {
            ServerArgs serverArgs = new ServerArgs().Parse(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "config.json")).Parse(args).Validate();

            EventLogOutputPipe eventLogOutputPipe = new EventLogOutputPipe();
            CompositeLogger    logger             = new CompositeLogger();

            if (serverArgs.EnableEventLog)
            {
                logger.AddLogger(new Logger(eventLogOutputPipe, eventLogOutputPipe));
            }

            if (serverArgs.EnablePipeLog)
            {
                try
                {
                    NamedPipeServer namedPipe       = new NamedPipeServer(serverArgs.LogPipeName);
                    Logger          namedPipeLogger = new Logger(namedPipe, namedPipe);
                    logger.AddLogger(namedPipeLogger);
                }
                catch (Exception e)
                {
                    logger.LogError("Failed to initialize named pipe logger: " + e.Message);
                }
            }

            if (serverArgs.Errors.Count > 0)
            {
                logger.LogError("Server arguments had errors.");
                foreach (string error in serverArgs.Errors)
                {
                    logger.LogError(error);
                }
                return;
            }

            // Load external modules
            PowerShellRunspace runspace = new PowerShellRunspace(logger);

            foreach (string externalModule in serverArgs.ExternalModules)
            {
                GeneratedModule module = new GeneratedModule(runspace);
                module.ModulePath = externalModule;
                CommandExecutionResult result = module.Load();
                if (result != null && result.HadErrors)
                {
                    logger.LogError(String.Format(CultureInfo.CurrentCulture, "Failed to load extra module: {0}", externalModule));
                    result.LogErrors(logger);
                    return;
                }
            }

            // Start test server
            JsonRpcPipe    jsonRpcPipe = new JsonRpcPipe(new StandardInputPipe(), new StandardOutputPipe());
            LiveTestServer server      = new LiveTestServer(new LiveTestServerStartParams()
            {
                Logger             = logger,
                Input              = jsonRpcPipe,
                Output             = jsonRpcPipe,
                CredentialFactory  = new LiveTestCredentialFactory(),
                RunspaceManager    = runspace,
                ModulePath         = serverArgs.ModulePath,
                TracingManager     = new ServiceTracingManager(),
                SpecificationPaths = serverArgs.SpecificationPaths,
                ObjectTransforms   = serverArgs.GetTransforms()
            });

            try
            {
                server.RunAsync().Wait();
            } catch (Exception ex)
            {
                logger.LogError("Failed to start server: " + ex.ToString());
            }

            // Wait until server exits (usually means the server ran into an internal error)
            while (server.IsRunning)
            {
                Thread.Sleep(2);
            }
        }