Search code examples
add-inxamarin-studio

Xamarin add-in reference installation location


I am writing a plugin that adds a project template. This template needs to use some files that the default templates use too.

I am looking for something like the ${ProjectName} variable but for the installation folder. Does something like this exist or is there some other workaround I can use?

Here is

<Files>
                <Directory name="Resources">
                    <RawFile name="Default.png" src="${Reference To Installation Folder}/Templates/iOS/Default.png" />
                    <RawFile name="Default@2x.png" src="${Reference To Installation Folder}/Templates/iOS/Default@2x.png" />
                </Directory>
</Files>

Solution

  • By installation folder I am assuming you mean where Xamarin Studio is installed.

    I am also assuming that you cannot distribute the files with your addin since they are part of Xamarin Studio and not available with just MonoDevelop.

    There is no property/parameter that specifies where Xamarin Studio is installed as far as I am aware.

    The src attribute in RawFile does not support having parameters being replaced so even if there was a parameter which pointed to where Xamarin Studio was installed it could not be used.

    So you are left with two options that I can think of:

    1. Implement a wizard for your project template.
    2. Implement your own RawFile template.

    A project template wizard would mean you could only support Xamarin Studio 5.9 and above. So I will ignore this for now. Both the above options are similar in how they are implemented.

    For your own version of the RawFile template you define the class to use in your addin.xml file:

    <Extension path = "/MonoDevelop/Ide/FileTemplateTypes">
        <FileTemplateType name = "RawFileNoExtension" class = "MyAddin.MyRawFileExtensionTemplate"/>
    </Extension>
    

    Then you can create your own file extension template class. Here is an example taken from the existing RawFileDescriptionTemplate but I have removed some error handling:

    public class MyRawFileExtensionTemplate : RawFileDescriptionTemplate
    {
        FilePath contentSrcFile;
    
        public override void Load (XmlElement filenode, FilePath baseDirectory)
        {
            base.Load (filenode, baseDirectory);
            var srcAtt = filenode.Attributes["src"];
    
            // TODO: Replace src with path to Xamarin Studio.
    
            contentSrcFile = FileService.MakePathSeparatorsNative (srcAtt.Value);
            contentSrcFile = contentSrcFile.ToAbsolute (baseDirectory);
        }
    
        public override Stream CreateFileContent (SolutionItem policyParent, Project project, string language,
            string fileName, string identifier)
        {
            return File.OpenRead (contentSrcFile);
        }
    }
    

    You would need to replace the TODO section with code to find where Xamarin Studio is installed. One way to do that would be to find a type in one of Xamarin Studio's assemblies and then get the assembly's location.