Set svn:eol-style to native
[mono.git] / mcs / tools / prj2make / MainMod.cs
1 // created on 4/3/04 at 6:27 a
2
3 // This program is free software; you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation; either version 2 of the License, or
6 // (at your option) any later version.
7 // 
8 // This program is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 // GNU Library General Public License for more details.
12 // 
13 // You should have received a copy of the GNU General Public License
14 // along with this program; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16
17 using System;
18 using System.IO;
19 using Mfconsulting.General.Prj2Make;
20
21 namespace Mfconsulting.General.Prj2Make.Cui
22 {
23     public class MainMod 
24     {
25         public string m_AppName = "prj2make-sharp";
26         public string m_WorkspaceFileName;
27         public string m_FileNamePath;
28         public string m_OutputMakefile;
29         
30         private bool m_IsUnix = false;
31         private bool m_IsMcs = true;
32         
33         // Determines the make file type/style
34         public bool IsUnix
35         {
36                 get { return m_IsUnix; }
37                 set { m_IsUnix = value; }
38         }
39         
40         // Determines what compiler to use MCS or CSC
41         public bool IsMcs
42         {
43                 get { return m_IsMcs; }
44                 set { m_IsMcs = value; }
45         }
46         
47         protected void MyInitializeComponents()
48         {
49         }
50         
51                 public MainMod (string inputFileName)
52                 {
53                         Mfconsulting.General.Prj2Make.Maker mkObj = null;
54
55                         MyInitializeComponents();
56                 
57                         if (inputFileName == null || inputFileName.Length < 1) 
58                         {
59                                 Console.WriteLine ("No input file has been specified.");
60                                 return;            
61                         }
62
63                         if (Path.GetExtension(inputFileName).ToUpper().CompareTo(".SLN") == 0)
64                         {
65                                 mkObj = new Mfconsulting.General.Prj2Make.Maker();
66                                 mkObj.CreateCombineFromSln(inputFileName);
67                                 return;
68                         }
69
70                         if (Path.GetExtension(inputFileName).ToUpper().CompareTo(".CSPROJ") == 0)
71                         {
72                                 mkObj = new Mfconsulting.General.Prj2Make.Maker();
73                                 mkObj.CreatePrjxFromCsproj(inputFileName);
74                                 return;
75                         }
76                 }
77                 
78                 // For command line handling
79         public MainMod (bool isNmake, bool isCsc, string WorkspaceFile)
80         {
81                 MyInitializeComponents();
82                 this.IsMcs = (isCsc == true) ? false : true;
83                 this.IsUnix = (isNmake == true) ? false : true;
84                 IniFromCommandLine(WorkspaceFile);
85         }
86         
87         void IniFromCommandLine(string WorkspaceFilename)
88         {
89                 m_WorkspaceFileName = WorkspaceFilename;
90                 m_FileNamePath = System.IO.Path.GetDirectoryName(m_WorkspaceFileName);
91                 m_OutputMakefile = System.IO.Path.Combine(m_FileNamePath, "Makefile");
92        
93                 if (this.IsUnix)
94                         m_OutputMakefile = System.IO.Path.Combine(m_FileNamePath, "Makefile");
95                 else
96                         m_OutputMakefile = System.IO.Path.Combine(m_FileNamePath, "Makefile.Win32");
97      
98                 // Actually follow through and genrate the contents of the Makefile
99                 // to be placed on the textview
100                 CreateMakefile();
101         }
102
103
104         protected void CreateMakefile()
105         {
106                 Mfconsulting.General.Prj2Make.Maker mkObj = null;
107                 string slnFile = null;
108                 
109                 if (this.m_WorkspaceFileName.Length < 1) {
110                         Console.WriteLine ("No input file has been specified.");
111                         return;            
112                 }
113                 else
114                         slnFile = m_WorkspaceFileName;
115                 
116                 mkObj = new Mfconsulting.General.Prj2Make.Maker();              
117                 SaveMakefileToDisk(mkObj.MakerMain(IsUnix, IsMcs, slnFile));
118         }
119     
120         protected void SaveMakefileToDisk (string MakeFileContents)
121         {
122                 FileStream fs = null;
123                 StreamWriter w = null;
124                 
125                 if (MakeFileContents.StartsWith ("Error") || MakeFileContents.StartsWith ("Notice")) {
126                         Console.WriteLine(MakeFileContents);
127                         return;                 
128                         } 
129                 
130                 if (m_OutputMakefile != null && m_OutputMakefile.Length > 1) {
131                         fs = new FileStream(m_OutputMakefile, FileMode.Create, FileAccess.Write);
132                         w = new StreamWriter(fs);
133                 }
134                 
135                 if (w != null) {
136                         w.WriteLine (MakeFileContents);
137                         w.Close();
138                 }
139         }
140     }    
141 }