Ejemplo n.º 1
0
        public override bool Execute()
        {
            var xamlatorAssembly    = System.IO.Path.GetFullPath(Path.Trim());
            var xamlatorAssemblyTmp = xamlatorAssembly + ".tmp";

            Log.LogMessage(MessageImportance.Normal, $"Weaving assembly {xamlatorAssembly}");
            AssemblyDefinition assemblyDef = AssemblyDefinition.ReadAssembly(xamlatorAssembly);
            var resources        = assemblyDef.MainModule.Resources;
            var selectedResource = resources.FirstOrDefault(x => x.Name == BuildConstants.IDE_IP_RESOURCE_NAME);

            if (selectedResource != null)
            {
                var    ips       = BuildNetworkUtils.DeviceIps();
                string ipsString = String.Join("-", ips);
                if (string.IsNullOrEmpty(ipsString))
                {
                    ipsString = "127.0.0.1";
                }
                Log.LogMessage(MessageImportance.Normal, $"XAMLator weaved with ips {String.Join("-", ips)}");
                var currentIps  = Encoding.ASCII.GetBytes(String.Join("\n", BuildNetworkUtils.DeviceIps()));
                var newResource = new EmbeddedResource(BuildConstants.IDE_IP_RESOURCE_NAME, selectedResource.Attributes, currentIps);
                resources.Remove(selectedResource);
                resources.Add(newResource);
                assemblyDef.Write(xamlatorAssemblyTmp);
            }
            else
            {
                Log.LogError($"Resource {BuildConstants.IDE_IP_RESOURCE_NAME} not found in assembly {xamlatorAssembly}");
            }
            assemblyDef.Dispose();

            File.Replace(xamlatorAssemblyTmp, xamlatorAssembly, xamlatorAssembly + ".backup");
            return(true);
        }
        public void The_xamlator_assembly_is_weaved_with_the_current_ip()
        {
            var task = new AssemblyWeaver();

            task.Path        = BuildConstants.XAMLATOR_ASSEMBLY;
            task.BuildEngine = Mock.Of <IBuildEngine>();
            task.Execute();

            var assembly = Assembly.LoadFrom(BuildConstants.XAMLATOR_ASSEMBLY);

            using (Stream stream = assembly.GetManifestResourceStream(BuildConstants.IDE_IP_RESOURCE_NAME))
                using (StreamReader reader = new StreamReader(stream))
                {
                    var ip = reader.ReadLine();
                    Assert.AreEqual(BuildNetworkUtils.DeviceIps().First(), ip);
                }
        }