Domanda

Sto usando CodeSmith il mio progetto in corso e sto cercando di capire un problema. Per il mio progetto CodeSmith (.csp) posso selezionare un'opzione di averlo aggiungere automaticamente tutti i file generati al progetto corrente (.csproj). Ma io voglio essere in grado di aggiungere l'output a più progetti (Csproj). C'è la possibilità all'interno di CodeSmith permettere questo? O c'è un buon modo per fare di programmazione che?

Grazie.

È stato utile?

Soluzione

I è stato in grado di capire un modo per rendere CodeSmith gestire questo problema automaticamente, quindi ho finito per scrivere un metodo personalizzato nel codice dietro file per gestire questa situazione.

Alcune note: - I file proj sono XML, e come quindi abbastanza facile da modificare, ma il nodo vero e proprio "ItemGroup" che contiene l'elenco dei file che sono inclusi nel progetto non è in realtà etichettato in alcun modo particolare. Ho finito per scegliere il nodo "ItemGroup" che ha "Contiene" nodi figlio, ma ci potrebbe essere un modo migliore per determinare quale si dovrebbe usare. - Vi consiglio di fare tutte le modifiche apportate ai file proj in una volta, invece di come si crea ogni file / aggiornamento. In caso contrario, se si avvia la generazione da Visual Studio, si potrebbe ottenere una marea di "La voce è cambiata, VUOI ricarica" -. Se i file sono sotto il controllo di origine (sono, giusto ?!), si sta andando ad avere bisogno di gestire il controllo dei file e li aggiunge al controllo del codice sorgente con la modifica dei file proj

Ecco (più o meno) il codice che ho usato per aggiungere un file al progetto:

/// <summary>
/// Adds the given file to the indicated project
/// </summary>
/// <param name="project">The path of the proj file</param>
/// <param name="projectSubDir">The subdirectory of the project that the 
/// file is located in, otherwise an empty string if it is at the project root</param>
/// <param name="file">The name of the file to be added to the project</param>
/// <param name="parent">The name of the parent to group the file to, an 
/// empty string if there is no parent file</param>
public static void AddFileToProject(string project, string projectSubDir, 
        string file, string parent)
{
    XDocument proj = XDocument.Load(project);

    XNamespace ns = "http://schemas.microsoft.com/developer/msbuild/2003";
    var itemGroup = proj.Descendants(ns + "ItemGroup").FirstOrDefault(x => x.Descendants(ns + "Compile").Count() > 0);

    if (itemGroup == null)
        throw new Exception(string.Format("Unable to find an ItemGroup to add the file {1} to the {0} project", project, file));

    //If the file is already listed, don't bother adding it again
    if(itemGroup.Descendants(ns + "Compile").Where(x=>x.Attribute("Include").Value.ToString() == file).Count() > 0)
        return; 

    XElement item = new XElement(ns + "Compile", 
                    new XAttribute("Include", Path.Combine(projectSubDir,file)));

    //This is used to group files together, in this case the file that is 
    //regenerated is grouped as a dependent of the user-editable file that
    //is not changed by the code generator
    if (string.IsNullOrEmpty(parent) == false)
        item.Add(new XElement(ns + "DependentUpon", parent));

    itemGroup.Add(item);

    proj.Save(project); 

}

Altri suggerimenti

Avete mai pensato di appena compilazione in un assembly condiviso (DLL) che può essere poi applicata da tutti i vostri progetti?

So che questo non può soddisfare le vostre esigenze, ma Vorrei assumere questo sarebbe uno dei modi migliori per raggiungere un unico fornitore che tutti i vostri progetti possono usare e anche una sola base di codice per mantenere, se necessario.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top