Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Tools/HeuristicLab.Subversion/SvnForm/SvnMain.cs @ 9234

Last change on this file since 9234 was 9234, checked in by ascheibe, 11 years ago

#2017 added tools that eyob worked on:

  • HL build center (HeuristicLab.Subversion)
  • a subwcrev replacement
  • 2 own HL apps for up- and downloading Hive jobs from HL files
File size: 16.3 KB
Line 
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.Collections.ObjectModel;
5using System.IO;
6using System.Linq;
7using System.Net;
8using System.Threading;
9using System.Windows.Forms;
10using Microsoft.Build.Exceptions;
11using Microsoft.Build.Execution;
12using Microsoft.Build.Framework;
13using SharpSvn;
14
15namespace SvnForm {
16  public partial class SvnMain : Form {
17    SvnClient client = new SvnClient();
18    Semaphore trunkStatus = new Semaphore(0, 1);
19    Semaphore branchStatus = new Semaphore(0, 1);
20    Semaphore handle = new Semaphore(0, 1);
21    private bool buildAborted = false;
22    private Thread builder;
23    private Thread build;
24
25    public SvnMain() {
26      InitializeComponent();
27    }
28
29    #region event handlers
30    private void SvnMain_Load(object sender, EventArgs e) {
31      tbxUrl.Text = "http://dev.heuristiclab.com/svn/hl/core/";
32      tbxBranches.Text = "http://dev.heuristiclab.com/svn/hl/core/branches/";
33      tbxDir.Text = @"c:\dir";
34      gbxLogin.Hide();
35      lblSolutions.Text = "Which Solution" + Environment.NewLine + "to be build";
36      tbxPwd.Text = "";
37      tbxUsername.Text = "";
38      lbxConfiguration.SelectedIndex = 0;
39      lbxPlatform.SelectedIndex = 0;
40      lbxBranches.SelectionMode = SelectionMode.MultiSimple;
41      LoadTreeView();
42      tabSvn.TabPages.Remove(tabProcess);
43      tabSvn.TabPages.Remove(tabBuild);
44    }
45
46    private void btnLoad_Click(object sender, EventArgs e) {
47      try {
48        lbxBranches.Items.Clear();
49        string branchUrl = tbxBranches.Text;
50        string uri = tbxUrl.Text;
51        Collection<SvnListEventArgs> contents;
52        if (client.GetList(new Uri(branchUrl), out contents)) {
53          foreach (SvnListEventArgs item in contents.Skip(1)) {
54            //add from the Uri to listbox
55            lbxBranches.Items.Add(item.Path);
56          }
57        }
58      }
59      catch {
60        lbxBranches.Items.Add("The Url is not correct or Authentication needed to acces this Url");
61      }
62    }
63
64    private void btnDown_Click(object sender, EventArgs e) {
65      if (lbxSolutions.SelectedIndex < lbxSolutions.Items.Count - 1) {
66        String Temp = lbxSolutions.Items[lbxSolutions.SelectedIndex].ToString();
67        lbxSolutions.Items[lbxSolutions.SelectedIndex] = lbxSolutions.Items[lbxSolutions.SelectedIndex + 1];
68        lbxSolutions.Items[lbxSolutions.SelectedIndex + 1] = Temp;
69        lbxSolutions.SelectedIndex++; // folow up by placing the selection again
70      }
71    }
72
73    private void btnUp_Click(object sender, EventArgs e) {
74      if (lbxSolutions.SelectedIndex > 0) {
75        String Temp = lbxSolutions.Items[lbxSolutions.SelectedIndex].ToString();
76        lbxSolutions.Items[lbxSolutions.SelectedIndex] = lbxSolutions.Items[lbxSolutions.SelectedIndex - 1];
77        lbxSolutions.Items[lbxSolutions.SelectedIndex - 1] = Temp;
78        lbxSolutions.SelectedIndex--; // folow up by placing the selection again
79      }
80    }
81
82    private void btnBrowse_Click(object sender, EventArgs e) {
83      if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) {
84        this.tbxDir.Text = folderBrowserDialog1.SelectedPath;
85      }
86    }
87
88    private void btnFinish_Click(object sender, EventArgs e) {
89      Application.Exit();
90    }
91
92    private void btnNext_Click(object sender, EventArgs e) {
93      tabSvn.TabPages.Remove(tabSvn.SelectedTab);
94      tabSvn.TabPages.Add(tabProcess);
95      Thread svn = new Thread(SvnTasks);
96      svn.Start();
97      btnNext2.Enabled = false;
98      Thread endSvn = new Thread(EndSvn);
99      endSvn.Start();
100
101    }
102
103    private void btnRefresh_Click(object sender, EventArgs e) {
104      LoadTreeView();
105    }
106
107    private void cbxAuthenticate_CheckedChanged(object sender, EventArgs e) {
108      if (cbxAuthenticate.Checked) {
109        gbxLogin.Show();
110      } else {
111        gbxLogin.Hide();
112      }
113    }
114
115    private void trvBranches_AfterSelect(object sender, TreeViewEventArgs e) {
116      string uri = tbxUrl.Text;
117      if ((uri.Substring(uri.Length - Math.Min(1, uri.Length)) == "/")) {        //to avoid double "//" i.e "http://xyz.com/files//abc//" on the Url
118        uri = uri.Remove(uri.Length - 1);                                        //e.g. dev.heuristiclab.com/svn/hl/core//branches/
119      }
120      string val = trvBranches.SelectedNode.Text;
121      if (trvBranches.SelectedNode.Parent == null) {
122        tbxBranches.Text = uri + "/" + val + "/";
123      } else {
124        string par = trvBranches.SelectedNode.Parent.Text;
125        tbxBranches.Text = uri + "/" + par + "/" + val + "/";
126      }
127    }
128
129    private void trvSolution_AfterCheck(object sender, TreeViewEventArgs e) {
130      if (e.Action != TreeViewAction.Unknown) {
131        if (e.Node.Nodes.Count > 0) {
132          /* Calls the CheckAllChildNodes method, passing in the current
133          Checked value of the TreeNode whose checked state changed. */
134          this.CheckAllChildNodes(e.Node, e.Node.Checked);
135        }
136      }
137      if (e.Node.Parent != null) {
138        if (e.Node.Checked) {
139          if (!lbxSolutions.Items.Contains(e.Node.Text)) {
140            lbxSolutions.Items.Add(e.Node.Text);
141          }
142        } else {
143          if (lbxSolutions.Items.Contains(e.Node.Text)) {
144            lbxSolutions.Items.Remove(e.Node.Text);
145          }
146        }
147      }
148      if (lbxSolutions.Items.Count > 0) {
149        btnNext2.Enabled = true;
150      }
151    }
152
153    private void btnBack_Click(object sender, EventArgs e) {
154      tabSvn.TabPages.Add(tabMain);
155      tabSvn.TabPages.Remove(tabSvn.SelectedTab);
156    }
157
158    private void btnNext2_Click(object sender, EventArgs e) {
159      buildLogTextBox.Clear();
160      tabSvn.TabPages.Add(tabBuild);
161      tabSvn.TabPages.Remove(tabSvn.SelectedTab);
162      build = new Thread(BuildAll);
163      build.Start();
164      ShowProgressBar();
165      DisableButton();
166    }
167
168    private void btnBack2_Click(object sender, EventArgs e) {
169      tabSvn.TabPages.Add(tabProcess);
170      tabSvn.TabPages.Remove(tabSvn.SelectedTab);
171      BuildManager.DefaultBuildManager.CancelAllSubmissions();
172    }
173
174    private void btnCancel_Click(object sender, EventArgs e) {
175      Application.Exit();
176    }
177
178    #endregion
179
180    #region Invoke
181    private void ShowProgressBar() {
182      if (InvokeRequired) {
183        MethodInvoker AssignMethodToControl = new MethodInvoker(ShowProgressBar);
184        prbSvn.Invoke(AssignMethodToControl);
185        prbBuild.Invoke(AssignMethodToControl);
186      } else {
187        prbSvn.Show();
188        prbBuild.Show();
189        prbSvn.Style = ProgressBarStyle.Marquee;
190        prbBuild.Style = ProgressBarStyle.Marquee;
191      }
192    }
193
194    private void HideProgressBar() {
195      if (InvokeRequired) {
196        MethodInvoker AssignMethodToControl = new MethodInvoker(HideProgressBar);
197        prbSvn.Invoke(AssignMethodToControl);
198      } else {
199        prbSvn.Hide();
200      }
201    }
202
203    private void HidePrbBuild() {
204      if (InvokeRequired) {
205        MethodInvoker AssignMethodToControl = new MethodInvoker(HidePrbBuild);
206        prbBuild.Invoke(AssignMethodToControl);
207      } else {
208        prbBuild.Hide();
209      }
210    }
211
212    private void UpdatelblStatus(string message) {
213      if (lblStatus.InvokeRequired) {
214        lblStatus.Invoke(new Action<string>(UpdatelblStatus), message);
215      } else {
216        lblStatus.Text = message;
217      }
218    }
219
220    private void UpdateBuildSln(string message) {
221      if (InvokeRequired) {
222        Invoke(new Action<string>(UpdateBuildSln), message);
223      } else
224        lblBuild.Text = message;
225    }
226
227    private void HidegbxBuild() {
228      if (gbxBuild.InvokeRequired) {
229        gbxBuild.Invoke(new MethodInvoker(HidegbxBuild));
230      } else {
231        gbxBuild.Hide();
232      }
233    }
234
235    private void ShowgbxBuild() {
236      if (gbxBuild.InvokeRequired) {
237        gbxBuild.Invoke(new MethodInvoker(ShowgbxBuild));
238      } else {
239        gbxBuild.Show();
240      }
241    }
242
243    private void UpdatelbxBranches() {
244      if (lbxBranches.InvokeRequired) {
245        MethodInvoker AssignMethodToControl = new MethodInvoker(UpdatelbxBranches);
246        lbxBranches.Invoke(AssignMethodToControl);
247      } else {
248        lbxBranches.SelectedItems.ToString();
249      }
250    }
251    private void EnableButton() {
252      if (btnFinish.InvokeRequired) {
253        MethodInvoker AssignMethodToControl = new MethodInvoker(EnableButton);
254        btnFinish.Invoke(AssignMethodToControl);
255      } else {
256        btnFinish.Enabled = true;
257      }
258    }
259
260    private void DisableButton() {
261      if (btnFinish.InvokeRequired) {
262        MethodInvoker AssignMethodToControl = new MethodInvoker(DisableButton);
263        btnFinish.Invoke(AssignMethodToControl);
264      } else {
265        btnFinish.Enabled = false;
266      }
267    }
268    public void AppendTextToBuildLog(string message) {
269      if (InvokeRequired) {
270        this.Invoke(new Action<string>(AppendTextToBuildLog), message);
271      } else {
272        buildLogTextBox.AppendText(message);
273      }
274    }
275
276    public void Login(string usrName, string password) {
277      client.Authentication.Clear();
278      client.Authentication.DefaultCredentials = new NetworkCredential(usrName, password);
279    }
280
281#endregion
282
283    #region treeview methods
284    private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked) {
285      foreach (TreeNode node in treeNode.Nodes) {
286        node.Checked = nodeChecked;
287        if (node.Nodes.Count > 0) {
288          // If the current node has child nodes, call the CheckAllChildsNodes method recursively.
289          this.CheckAllChildNodes(node, nodeChecked);
290        }
291      }
292    }
293
294    private void LoadTreeView() {
295      try {
296        trvBranches.Nodes.Clear();
297        string branchUrl = tbxBranches.Text;
298        TreeNode node = new TreeNode();
299        string uri = tbxUrl.Text;
300        if ((uri.Substring(uri.Length - Math.Min(1, uri.Length)) != "/")) {
301          uri = uri + "/";
302        }
303        Collection<SvnListEventArgs> contents;
304        Collection<SvnListEventArgs> childContents;
305        Login(tbxUsername.Text, tbxPwd.Text);
306        if (client.GetList(new Uri(uri), out contents)) {
307          foreach (SvnListEventArgs item in contents.Skip(1)) {
308            node = new TreeNode(item.Path);
309            trvBranches.Nodes.Add(node);
310            if (client.GetList(new Uri(uri + item.Path), out childContents)) {
311              foreach (SvnListEventArgs items in childContents.Skip(1)) {
312                node.Nodes.Add(items.Path);
313              }
314            }
315          }
316        }
317      }
318      catch {
319        lbxBranches.Items.Clear();
320        lbxBranches.Items.Add("The Url is not correct or Authentication needed to acces this Url");
321      }
322    }
323
324    private void LoadSolutionsTreeView() {
325      if (tbxDir.InvokeRequired) {
326        tbxDir.Invoke(new Action(LoadSolutionsTreeView));
327      } else {
328        trvSolution.Nodes.Clear();
329        string dir = tbxDir.Text;
330        string[] solutions = { "trunk", "branches" };
331        TreeNode node = new TreeNode();
332        foreach (string sol in solutions) {
333          node = new TreeNode(sol);
334          trvSolution.Nodes.Add(node);
335          if (sol == "trunk") {
336            foreach (string filName in (Directory.GetFiles(dir + @"\" + sol + @"\sources", "*.sln"))) {
337              node.Nodes.Add(filName);
338            }
339          } else {
340            foreach (string filName in (Directory.GetFiles(dir + @"\" + sol, "*.sln", SearchOption.AllDirectories))) {
341              node.Nodes.Add(filName);
342            }
343          }
344        }
345        trvSolution.ExpandAll();
346      }
347    }
348    #endregion
349
350    #region svnTasks
351
352    private void SvnTasks() {
353      try {
354        HidegbxBuild();
355        if (lbxBranches.InvokeRequired) {
356          lbxBranches.Invoke(new Action(SvnTasks));
357        } else {
358          ShowProgressBar();
359          Thread tr = new Thread(new ThreadStart(SvnTrunk));
360          Thread ch = new Thread(CheckoutBranches);
361          tr.IsBackground = true;
362          ch.IsBackground = true;
363          tr.Start();
364          ArrayList lstSelItems = new ArrayList();
365          UpdatelbxBranches();
366          for (int i = 0; i < lbxBranches.SelectedItems.Count; i++) {
367            lstSelItems.Add(lbxBranches.SelectedItems[i]);
368          }
369          ch.Start(lstSelItems);
370          if (tr.IsAlive || ch.IsAlive) {
371            ShowProgressBar();
372          } else {
373            HideProgressBar();
374          }
375          UpdatelblStatus("Cleaning and Updating the directory... ");
376        }
377      }
378      catch (Exception e) {
379        UpdatelblStatus(e.Message);
380      }
381    }
382
383    public void SvnTrunk() {
384      try {
385        SvnClient client = new SvnClient();
386        string trunk = tbxUrl.Text + "/trunk";
387        string dir = tbxDir.Text;
388        if (System.IO.Directory.Exists(dir + @"\trunk\")) {
389          client.CleanUp(Path.Combine(dir, @"\trunk"));
390          SvnUpdateResult r;
391          client.Update(dir + @"\trunk\", out r);
392        } else {
393          client.CheckOut(new Uri(trunk), dir +  @"\trunk");
394        }
395      }
396      catch (Exception e) {
397        UpdatelblStatus(e.Message);
398      }
399      trunkStatus.Release();
400    }
401
402    public void CheckoutBranches(object selectedItems) {
403      try {
404        string branches = tbxBranches.Text;
405        string dir = tbxDir.Text;
406        if (!Directory.Exists(dir + @"\branches\")) {
407          Directory.CreateDirectory(dir + @"\branches\");
408        }
409        UpdatelbxBranches();
410        foreach (Object selectedItem in (ArrayList)selectedItems) {
411          String br;
412          br = selectedItem as String;
413          if (!System.IO.Directory.Exists(dir + @"\branches\" + br)) {
414            client.CheckOut(new Uri(branches + br), dir+ @"\branches\"+ br);
415          } else {
416              client.CleanUp(dir + @"\branches\" + br + "\\");
417            SvnUpdateResult r;
418            client.Update(dir + @"\branches\" + br + "\\", out r);
419          }
420        }
421      }
422      catch (Exception e) {
423        UpdatelblStatus(e.Message);
424      }
425      branchStatus.Release();
426    }
427
428    private void EndSvn() {
429      trunkStatus.WaitOne();
430      branchStatus.WaitOne();
431      UpdatelblStatus("");
432      HideProgressBar();
433      ShowgbxBuild();
434      Thread solTree = new Thread(LoadSolutionsTreeView);
435      solTree.Start();
436    }
437
438    #endregion
439
440    #region Build Solution
441
442    private void Build(string fileName) {
443      if (InvokeRequired) {
444        Invoke(new Action<string>(Build), fileName);
445      } else {
446        try {
447          BuildLogger bl = new BuildLogger(this);
448          List<ILogger> blList = new List<ILogger>();
449          blList.Add(bl);
450          string dir = tbxDir.Text.ToString();
451          string config = lbxConfiguration.SelectedItem.ToString();
452          string platform = lbxPlatform.SelectedItem.ToString();
453          var GlobalProperty = new Dictionary<string, string>();
454          GlobalProperty.Add("Configuration", config);
455          GlobalProperty.Add("Platform", platform);
456          BuildManager.DefaultBuildManager.BeginBuild(new BuildParameters() {
457            DetailedSummary = false,
458            Loggers = blList
459          });
460          BuildRequestData request = new BuildRequestData(fileName, GlobalProperty, null, new string[] { "Build" }, null);
461          BuildSubmission submission = BuildManager.DefaultBuildManager.PendBuildRequest(request);
462          submission.ExecuteAsync(BuildCompletedCallback, null);
463        }
464        catch (Exception e) {
465          UpdatelblStatus(e.Message);
466        }
467      }
468    }
469
470
471    private void BuildCompletedCallback(BuildSubmission submission) {
472      BuildManager.DefaultBuildManager.EndBuild();
473      if (submission.BuildResult.Exception is BuildAbortedException) {
474        buildAborted = true;
475      }
476      handle.Release();
477    }
478
479    private void BuildAll() {
480      List<string> files = new List<string>();
481      foreach (string file in lbxSolutions.Items) files.Add(file);
482      foreach (string file in files) {
483        builder = new Thread(() => Build(file));
484        builder.Start();
485        UpdateBuildSln("Building " + file);
486        handle.WaitOne();
487        if (buildAborted) {
488          buildAborted = false;
489          break;
490        }
491      }
492      UpdateBuildSln("Finished building");
493      HidePrbBuild();
494      EnableButton();
495    }
496   
497    #endregion
498
499  }
500}
Note: See TracBrowser for help on using the repository browser.