コード例 #1
0
ファイル: Processor.cs プロジェクト: jake-bladt/Fody
    void Inner()
    {
        ValidateProjectPath();

        ValidateAssemblyPath();

        ConfigFiles = ConfigFileFinder.FindWeaverConfigs(SolutionDirectory, ProjectDirectory, Logger);

        if (!ShouldStartSinceFileChanged())
        {
            if (!CheckForWeaversXmlChanged())
            {
                FindWeavers();

                if (WeaversHistory.HasChanged(Weavers.Select(x => x.AssemblyPath)))
                {
                    Logger.LogError("A re-build is required because a weaver has changed.");
                }
            }
            return;
        }

        ValidateSolutionPath();

        FindWeavers();

        if (Weavers.Count == 0)
        {
            Logger.LogWarning(@"No configured weavers. It is possible no weavers have been installed or a weaver has been installed into a project type that does not support install.ps1. It may be necessary to manually add that weaver to FodyWeavers.xm;. eg.
<Weavers>
    <WeaverName/>
</Weavers>
see https://github.com/Fody/Fody/wiki/SampleUsage");
            return;
        }
        lock (locker)
        {
            ExecuteInOwnAppDomain();
        }

        FlushWeaversXmlHistory();
    }
コード例 #2
0
    void Inner()
    {
        ValidateSolutionPath();
        ValidateProjectPath();
        ValidateAssemblyPath();

        ConfigFiles = ConfigFileFinder.FindWeaverConfigFiles(WeaverConfiguration, SolutionDirectory, ProjectDirectory, Logger).ToList();

        if (!ConfigFiles.Any())
        {
            ConfigFiles = new List <WeaverConfigFile>
            {
                ConfigFileFinder.GenerateDefault(ProjectDirectory, Weavers, GenerateXsd)
            };
            Logger.LogWarning($"Could not find a FodyWeavers.xml file at the project level ({ProjectDirectory}). A default file has been created. Please review the file and add it to your project.");
        }

        ConfigEntries = ConfigFileFinder.ParseWeaverConfigEntries(ConfigFiles);

        var extraEntries = ConfigEntries.Values
                           .Where(entry => !entry.ConfigFile.AllowExtraEntries && !Weavers.Any(weaver => string.Equals(weaver.ElementName, entry.ElementName)))
                           .ToArray();

        const string missingWeaversHelp = "Add the desired weavers via their nuget package.";

        if (extraEntries.Any())
        {
            throw new WeavingException($"No weavers found for the configuration entries {string.Join(", ", extraEntries.Select(e => e.ElementName))}. " + missingWeaversHelp);
        }

        if (Weavers.Count == 0)
        {
            throw new WeavingException("No weavers found. " + missingWeaversHelp);
        }

        foreach (var weaver in Weavers)
        {
            if (ConfigEntries.TryGetValue(weaver.ElementName, out var config))
            {
                weaver.Element             = config.Content;
                weaver.ConfigurationSource = config.ConfigFile.FilePath ?? "MSBuild property";
                weaver.ExecutionOrder      = config.ExecutionOrder;
            }
            else
            {
                Logger.LogWarning($"No configuration entry found for the installed weaver {weaver.ElementName}. This weaver will be skipped. You may want to add this weaver to your FodyWeavers.xml");
            }
        }

        ConfigFileFinder.EnsureSchemaIsUpToDate(ProjectDirectory, Weavers, GenerateXsd);

        Weavers = Weavers
                  .Where(weaver => weaver.Element != null)
                  .OrderBy(weaver => weaver.ExecutionOrder)
                  .ToList();

        lock (mutex)
        {
            ExecuteInOwnAssemblyLoadContext();
        }
    }
コード例 #3
0
    public void ShouldCreateXsd()
    {
        File.WriteAllText(xmlPath, @"
<Weavers>
  <TestWeaver />
</Weavers>
");

        File.WriteAllText(Path.Combine(testDir, "WeaverWithSchema.Fody.xcf"), @"
<xs:complexType xmlns:xs=""http://www.w3.org/2001/XMLSchema"">
  <xs:attribute name=""TestAttribute"" type=""xs:string"" />
</xs:complexType>
");

        var weavers = new[]
        {
            new WeaverEntry {
                AssemblyPath = @"something\TestWeaver.Fody.dll"
            },
            new WeaverEntry {
                AssemblyPath = Path.Combine(testDir, "WeaverWithSchema.Fody.dll")
            }
        };

        var configFiles = ConfigFileFinder.FindWeaverConfigFiles(Guid.NewGuid().ToString(), testDir, new Mock <BuildLogger>().Object).ToArray();

        ConfigFileFinder.EnsureSchemaIsUpToDate(testDir, weavers, true);

        Assert.Single(configFiles);
        Assert.False(configFiles[0].IsGlobal);
        Assert.Equal(xmlPath, configFiles[0].FilePath);

        Assert.True(File.Exists(xsdPath));

        var xml = XDocumentEx.Load(xmlPath);

        Assert.NotNull(xml.Root);
        Assert.Equal("FodyWeavers.xsd", xml.Root.Attribute(schemaInstanceNamespace + "noNamespaceSchemaLocation")?.Value);

        var xsd = XDocumentEx.Load(xsdPath);

        Assert.NotNull(xsd.Root);
        var elements = xsd.Root.Descendants(schemaNamespace + "all").First().Elements().ToList();

        Assert.Equal(2, elements.Count);

        var defaultElem = elements[0];

        Assert.Equal("element", defaultElem.Name.LocalName);
        Assert.Equal("TestWeaver", defaultElem.Attribute("name")?.Value);
        Assert.Equal("xs:anyType", defaultElem.Attribute("type")?.Value);
        Assert.Equal("0", defaultElem.Attribute("minOccurs")?.Value);
        Assert.Equal("1", defaultElem.Attribute("maxOccurs")?.Value);

        var elemWithSchema = elements[1];

        Assert.Equal("element", elemWithSchema.Name.LocalName);
        Assert.Equal("WeaverWithSchema", elemWithSchema.Attribute("name")?.Value);
        Assert.Null(elemWithSchema.Attribute("type"));
        Assert.Equal("0", elemWithSchema.Attribute("minOccurs")?.Value);
        Assert.Equal("1", elemWithSchema.Attribute("maxOccurs")?.Value);

        var elemWithSchemaType = Assert.Single(elemWithSchema.Elements());

        Assert.NotNull(elemWithSchemaType);
        Assert.Equal("complexType", elemWithSchemaType.Name.LocalName);

        var elemWithSchemaTypeAttr = Assert.Single(elemWithSchemaType.Elements());

        Assert.NotNull(elemWithSchemaTypeAttr);
        Assert.Equal("attribute", elemWithSchemaTypeAttr.Name.LocalName);
        Assert.Equal("TestAttribute", elemWithSchemaTypeAttr.Attribute("name")?.Value);
    }
コード例 #4
0
    void Inner()
    {
        ValidateSolutionPath();
        ValidateProjectPath();
        ValidateAssemblyPath();

        ConfigFiles = ConfigFileFinder.FindWeaverConfigFiles(SolutionDirectory, ProjectDirectory, Logger).ToList();

        if (!ConfigFiles.Any())
        {
            ConfigFiles = new List <WeaverConfigFile>
            {
                ConfigFileFinder.GenerateDefault(ProjectDirectory, Weavers, GenerateXsd)
            };
            Logger.LogWarning($"Could not find a FodyWeavers.xml file at the project level ({ProjectDirectory}). A default file has been created. Please review the file and add it to your project.");
        }

        ConfigEntries = ConfigFileFinder.ParseWeaverConfigEntries(ConfigFiles);

        var extraEntries = ConfigEntries.Values
                           .Where(entry => !entry.ConfigFile.IsGlobal && !Weavers.Any(weaver => string.Equals(weaver.ElementName, entry.ElementName)))
                           .ToArray();

        const string missingWeaversHelp = "Add the desired weavers via their nuget package; see https://github.com/Fody/Fody/wiki on how to migrate InSolution, custom or legacy weavers.";

        if (extraEntries.Any())
        {
            throw new WeavingException($"No weavers found for the configuration entries {string.Join(", ", extraEntries.Select(e => e.ElementName))}. " + missingWeaversHelp);
        }

        if (Weavers.Count == 0)
        {
            throw new WeavingException("No weavers found. " + missingWeaversHelp);
        }

        foreach (var weaver in Weavers)
        {
            if (ConfigEntries.TryGetValue(weaver.ElementName, out var config))
            {
                weaver.Element        = config.Content;
                weaver.ExecutionOrder = config.ExecutionOrder;
            }
            else
            {
                Logger.LogWarning($"No configuration entry found for the installed weaver {weaver.ElementName}. This weaver will be skipped. You may want to add this weaver to your FodyWeavers.xml");
            }
        }

        ConfigFileFinder.EnsureSchemaIsUpToDate(ProjectDirectory, Weavers, GenerateXsd);

        Weavers = Weavers
                  .Where(weaver => weaver.Element != null)
                  .OrderBy(weaver => weaver.ExecutionOrder)
                  .ToList();

        if (TargetAssemblyHasAlreadyBeenProcessed())
        {
            if (WeaversConfigHistory.HasChanged(ConfigFiles) || WeaversHistory.HasChanged(Weavers.Select(x => x.AssemblyPath)))
            {
                Logger.LogError("A re-build is required because a weaver has changed.");

                return;
            }
        }

        lock (mutex)
        {
            ExecuteInOwnAssemblyLoadContext();
        }

        WeaversConfigHistory.RegisterSnapshot(ConfigFiles);
    }