aboutsummaryrefslogtreecommitdiff
path: root/src/Projects/ModuleProject.cs
blob: 4643aa08d7a49b815ae9a07c2b7a4f504486965b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.IO;
using System.Threading.Tasks;

using VNLib.Tools.Build.Executor.Model;
using VNLib.Tools.Build.Executor.Extensions;

namespace VNLib.Tools.Build.Executor.Projects
{
    internal abstract class ModuleProject : IProject
    {
        ///<inheritdoc/>
        public FileInfo ProjectFile { get; }

        ///<inheritdoc/>
        public string ProjectName { get; protected set; }

        ///<inheritdoc/>
        public abstract IProjectData ProjectData { get; }

        ///<inheritdoc/>
        public bool UpToDate { get; set; }

        ///<inheritdoc/>
        public DirectoryInfo WorkingDir { get; protected set; }
        
        ///<inheritdoc/>
        public TaskfileVars TaskVars { get; protected set; }

        ///<inheritdoc/>
        public string? TaskfileName { get; protected set; }

        /// <summary>
        /// Gets the package info file for the project
        /// </summary>
        protected virtual FileInfo? PackageInfoFile { get; }

        public ModuleProject(FileInfo projectFile, string? projectName = null)
        {
            ProjectFile = projectFile;

            //Default project name to the file name
            ProjectName = projectName ?? Path.GetFileNameWithoutExtension(ProjectFile.Name);

            //Default up-to-date false
            UpToDate = false;

            //Default working dir to the project file's directory
            WorkingDir = ProjectFile.Directory!;

            TaskVars = null!;
        }
       
        ///<inheritdoc/>
        public virtual async Task LoadAsync(TaskfileVars vars)
        {
            TaskVars = vars;

            await LoadProjectDom();

            //Set some local environment variables

            //Set local environment variables
            TaskVars.Set("PROJECT_NAME", ProjectName);
            TaskVars.Set("PROJECT_DIR", WorkingDir.FullName);
            TaskVars.Set("IS_PROJECT", bool.TrueString);

            //Store project vars
            TaskVars.Set("PROJ_VERSION", ProjectData.VersionString ?? string.Empty);
            TaskVars.Set("PROJ_DESCRIPTION", ProjectData.Description ?? string.Empty);
            TaskVars.Set("PROJ_AUTHOR", ProjectData.Authors ?? string.Empty);
            TaskVars.Set("PROJ_COPYRIGHT", ProjectData.Copyright ?? string.Empty);
            TaskVars.Set("PROJ_COMPANY", ProjectData.CompanyName ?? string.Empty);
            TaskVars.Set("RPOJ_URL", ProjectData.RepoUrl ?? string.Empty);

            TaskVars.Set("SAFE_PROJ_NAME", this.GetSafeProjectName());
        }

        /// <summary>
        /// Loads the project's XML dom from its msbuild project file
        /// </summary>
        /// <returns>A task that resolves when the dom is built</returns>
        public async Task LoadProjectDom()
        {
            using MemoryStream ms = new();

            FileInfo dom = ProjectFile;

            if (PackageInfoFile?.Exists == true)
            {
                dom = PackageInfoFile;
            }

            try
            {
                //Get the project file
                await using (FileStream projData = dom.OpenRead())
                {
                    await projData.CopyToAsync(ms);
                }

                //reset stream
                ms.Seek(0, SeekOrigin.Begin);

                //Load the project dom
                ProjectData.Load(ms);
            }
            catch (Exception ex)
            {
                throw new Exception($"Failed to load project dom file {dom.FullName}", ex);
            }
        }

        public abstract void Dispose();

        public override string ToString() => ProjectName;
    }
}