Inheritance: IEnvironmentProvider
        public EnvironmentEncrypter(string startFolder, string aesKey)
        {
            _envProvider = new EnvironmentProvider();
            _aesKey = aesKey;

            _envProvider.Initialize(startFolder);
        }
        public override bool Execute()
        {
            LogManager.LogFactory = new BuildLogFactory(Log);

            Log.LogCommandLine("Transform all project files within the current solution...");

            try
            {
                var envProvider = new EnvironmentProvider(Directory);

                var templateEngine = new TemplateEngine();
                templateEngine.TransformDirectory(Directory, envProvider.GetEnvironment(Environment));
            }
            catch (DirectoryNotFoundException)
            {
                Log.LogError(".powerdeploy folder not found for project " + Directory + "! :(");
                return false;
            }
            catch (FileNotFoundException exception)
            {
                Log.LogError(exception.Message);
                return false;
            }

            return true;
        }
        public void Find_Environment_With_Dir_Not_Existing()
        {
            using (var workDir = new TestFolder(Environment.SpecialFolder.LocalApplicationData))
            {
                workDir.AddFolder("dir1");
                workDir.AddFolder("dir1/subdir1");

                var target = new EnvironmentProvider(Path.Combine(workDir.DirectoryInfo.FullName, "dir1/subdir1"));
                target.GetEnvironment("unittest");
            }
        }
        public void Find_Environment()
        {
            var target = new EnvironmentProvider();

            target.Initialize(@"samples\PowerDeploy.Sample.XCopy".MapVcsRoot());
            var result = target.GetEnvironment("unittest");

            Assert.IsNotNull(result);
            Assert.AreEqual("unittest", result.Name);
            Assert.AreEqual("Jack", result["Firstname"].Value);
            Assert.AreEqual("Bauer", result["Lastname"].Value);
        }
        public void Execute(IPackage package, string packagePath)
        {
            var dialog = new OpenFileDialog();
            var result = dialog.ShowDialog();

            if (result == DialogResult.OK)
            {
                var provider = new EnvironmentProvider();

                var packageManager = new PackageManager(provider);
                packageManager.ConfigurePackage(packagePath, dialog.FileNames[0], @"c:\temp\packageexplorer");
            }
        }
        public void Find_Environment_In_Same_Dir()
        {
            var xml = @"<?xml version=""1.0""?>
                        <environment name=""local"" description=""Used for unit tests, not a real environment"">
                          <variable name=""Name"" value=""Tobi"" />
                          <variable name=""Jack"" value=""Bauer"" />
                        </environment>";

            using (var workDir = new TestFolder(Environment.SpecialFolder.LocalApplicationData))
            {
                workDir.AddFolder("dir1");
                workDir.AddFolder("dir1/subdir1");
                workDir.AddFolder(".powerdeploy");
                workDir.AddFile(".powerdeploy/unittest.xml", xml);

                var target = new EnvironmentProvider(workDir.DirectoryInfo.FullName);
                var result = target.GetEnvironment("unittest");

                Assert.AreEqual("local", result.Name);
                Assert.AreEqual("Tobi", result["Name"].Value);
                Assert.AreEqual("Bauer", result["Jack"].Value);
            }
        }
        public void Deserialize_Environment_With_Include()
        {
            using (var dir = new TestFolder())
            {
                dir.AddFile("common.xml", @"<?xml version=""1.0""?>
                                            <environment name=""common"" description=""just a base"">
                                                <variable name=""firstname"" value=""jack"" />
                                                <variable name=""lastname"" value=""bauer"" />
                                            </environment>");

                dir.AddFile("testenv.xml", @"<?xml version=""1.0""?>
                                            <environment name=""testenv"" description=""testenv which includes common.xml"" include=""common.xml"">
                                                <variable name=""lastname"" value=""zuercher"" />
                                            </environment>");

                var envprovider = new EnvironmentProvider();
                var result = envprovider.GetEnvironmentFromFile(Path.Combine(dir.DirectoryInfo.FullName, "testenv.xml"));

                Assert.AreEqual(3, result.Variables.Count);
                Assert.AreEqual("TESTENV", result["env"].Value);
                Assert.AreEqual("jack", result["firstname"].Value);
                Assert.AreEqual("zuercher", result["lastname"].Value);
            }
        }
        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;
                }
            }
        }