private EnvironmentVariablesDto GetEnvironment(string name, bool fetchVariables)
        {
            using (var session = DocumentStore.OpenSession())
            {
                var environment = session.Query <Environment>().FirstOrDefault(e => e.Name == name);

                if (environment == null)
                {
                    throw HttpError.NotFound("Environment {0} not found.".Fmt(name));
                }

                using (var workspace = new Workspace(FileSystem, ServerSettings))
                {
                    var result = new EnvironmentVariablesDto()
                    {
                        Environment = environment.ToDto()
                    };

                    if (fetchVariables)
                    {
                        workspace.UpdateSources();

                        var provider = new EnvironmentProvider();

                        try
                        {
                            var serializedEnvironment = provider.GetEnvironmentFromFile(Path.Combine(workspace.EnviornmentPath, name + ".xml"));

                            var resolver = new VariableResolver(serializedEnvironment.Variables);

                            result.Variables = new List <VariableDto>();
                            result.Variables.AddRange(serializedEnvironment.Variables.Select(v => new VariableDto()
                            {
                                Name     = v.Name,
                                Value    = v.Value,
                                Resolved = resolver.TransformVariables(v.Value)
                            }));

                            if (resolver.VariableUsageList.Any(v => v.IsMissingValue))
                            {
                                result.MissingVariables =
                                    new List <string>(resolver.VariableUsageList.Where(v => v.IsMissingValue).Select(v => v.Variable.Name));
                            }
                        }
                        catch (FileNotFoundException e)
                        {
                            result.Variables = new List <VariableDto>();
                            result.Warning   = "No xml file found for this environment!";
                        }
                    }

                    return(result);
                }
            }
        }
        private EnvironmentVariablesDto GetEnvironment(string name, bool fetchVariables)
        {
            using (var session = DocumentStore.OpenSession())
            {
                var environment = session.Query<Environment>().FirstOrDefault(e => e.Name == name);

                if (environment == null)
                {
                    throw HttpError.NotFound("Environment {0} not found.".Fmt(name));
                }

                using (var workspace = new Workspace(FileSystem, ServerSettings))
                {
                    var result = new EnvironmentVariablesDto()
                    {
                        Environment = environment.ToDto()
                    };

                    if (fetchVariables)
                    {
                        workspace.UpdateSources();

                        var provider = new EnvironmentProvider();

                        try
                        {
                            var serializedEnvironment = provider.GetEnvironmentFromFile(Path.Combine(workspace.EnviornmentPath, name + ".xml"));

                            var resolver = new VariableResolver(serializedEnvironment.Variables);

                            result.Variables = new List<VariableDto>();
                            result.Variables.AddRange(serializedEnvironment.Variables.Select(v => new VariableDto()
                            {
                                Name = v.Name,
                                Value = v.Value,
                                Resolved = resolver.TransformVariables(v.Value)
                            }));

                            if (resolver.VariableUsageList.Any(v => v.IsMissingValue))
                            {
                                result.MissingVariables =
                                    new List<string>(resolver.VariableUsageList.Where(v => v.IsMissingValue).Select(v => v.Variable.Name));
                            }
                        }
                        catch (FileNotFoundException e)
                        {
                            result.Variables = new List<VariableDto>();
                            result.Warning = "No xml file found for this environment!";
                        }
                    }

                    return result;
                }
            }
        }