Skip to content
This repository has been archived by the owner on Nov 21, 2020. It is now read-only.

Commit

Permalink
VS Extension, Configuration Sources (#32)
Browse files Browse the repository at this point in the history
* Visual studio extension to use initializr templates; Issue #31

* Add Config Server

* Placeholder data

* RandomValueConfig
  • Loading branch information
Hananiel Sarella authored Sep 11, 2019
1 parent 9963708 commit cc30740
Show file tree
Hide file tree
Showing 488 changed files with 268,835 additions and 152 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Steeltoe Initializr

Master: ![image](https://dev.azure.com/SteeltoeOSS/Steeltoe/_apis/build/status/SteeltoeOSS.initializr?branchName=master)
Dev: ![image](https://dev.azure.com/SteeltoeOSS/Steeltoe/_apis/build/status/SteeltoeOSS.initializr?branchName=mdev)
Dev: ![image](https://dev.azure.com/SteeltoeOSS/Steeltoe/_apis/build/status/SteeltoeOSS.initializr?branchName=dev)

Steeltoe Initializr provides an extensible API to generate quickstart projects. It provides a simple web UI to configure the project to generate and endpoints that you can use via plain HTTP.

Expand Down
18 changes: 18 additions & 0 deletions SteeltoeVsix/NewSteeltoeProject/DiscoveryDialog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.PlatformUI;

namespace NewSteeltoeProject
{
public class DiscoveryDialog : DialogWindow
{
internal DiscoveryDialog()
{
HasMaximizeButton = HasMinimizeButton = true;

}
}
}
46 changes: 46 additions & 0 deletions SteeltoeVsix/NewSteeltoeProject/InitializrControl.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<local:DiscoveryDialog x:Class="NewSteeltoeProject.InitializrControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:NewSteeltoeProject"
mc:Ignorable="d" >
<Grid VerticalAlignment="Top" Height="Auto">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition Height="680"></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition Width="100"></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>

</Grid.ColumnDefinitions>

<Label Name="lblProjectName" Grid.Column="0" Grid.Row="0">Project Name:</Label>
<TextBox Name="txtProjectName" Grid.Column="1" Grid.Row="0">SteeltoeProject</TextBox>
<Label Name="lblDependencies" Margin="0 10 0 0" Grid.Column="0" Grid.Row="1">Dependencies:</Label>
<ListView x:Name="lstDependencies" Margin="0 10 0 0" ItemsSource="{Binding Dependencies}" Grid.ColumnSpan="2" Grid.Column="1" Grid.Row="1">
<ListView.View>
<GridView>
<GridViewColumn Header="Dependencies" Width="400">
<GridViewColumn.CellTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding IsSelected}">
<WrapPanel Orientation="Horizontal" ItemHeight="20" ItemWidth="400">
<TextBlock FontWeight="Bold" Text="{Binding Name}" />
<TextBlock Text="{Binding Description}" />
</WrapPanel>
</CheckBox>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<Button x:Name="btn2" Content="Create project"
Click="OnClick2" ClickMode="Press" Grid.Column="1" Grid.Row="2"
Foreground="Blue" Margin="0 10" Height="50"/>
</Grid>
</local:DiscoveryDialog>
188 changes: 188 additions & 0 deletions SteeltoeVsix/NewSteeltoeProject/InitializrControl.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
using EnvDTE;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Media;
using System.Diagnostics;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Settings;
using Microsoft.VisualStudio.Shell.Settings;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
using System.Net;
using System.Linq;
using Microsoft;

namespace NewSteeltoeProject
{
/// <summary>
/// Interaction logic for UserControl1.xaml
/// </summary>
public partial class InitializrControl : DiscoveryDialog
{
public ObservableCollection<Dependency> Dependencies { get; set; }


public InitializrControl()
{
InitializeComponent();
Dependencies = new ObservableCollection<Dependency>();
//Dependencies.Add(new Dependency { Name = "Hystrix " });
//Dependencies.Add(new Dependency { Name = "Actuator " });
//Dependencies.Add(new Dependency { Name = "SqlServer " });
//Dependencies.Add(new Dependency { Name = "Dynamic Logging " });

// TODO: Pull dependencies from pws url
var deps = GetDependenciesAsync().Result;
foreach(var dep in deps)
{
Dependencies.Add(dep);
}

// this.browser.AllowNavigation = true;
this.DataContext = this;
}



void OnClick2(object sender, RoutedEventArgs e)
{
ThreadHelper.ThrowIfNotOnUIThread();
btn2.Foreground = new SolidColorBrush(Colors.Green);


var stringDependencies = GetSelectedDependencies();
bool done = false;
while (!done)
{
var proc = new System.Diagnostics.Process();
var arguments = "new Steeltoe-WebApi " + stringDependencies;
string workingDir = GetSettingsPath() + Path.DirectorySeparatorChar + txtProjectName.Text;
if(!Directory.Exists(workingDir))
{
Directory.CreateDirectory(workingDir);
}
else
{
throw new Exception("Project exists; select a different name");
}
string text = ExecuteProcess(proc, "dotnet", arguments, out var errorText, workingDir);

if (text.Contains("No templates matched the input template name:"))
{
arguments = "new -i steeltoe.templates::2.2.0 --nuget-source https://www.myget.org/F/steeltoedev/api/v3/index.json";
ExecuteProcess(proc, "dotnet", arguments, out var errorText2, "");
//TODO: check for install failure
}
else if (text.Contains("was created successfully."))
{
DTE dte = (DTE)ServiceProvider.GlobalProvider.GetService(typeof(DTE));
Assumes.Present(dte);
dte.ExecuteCommand("File.OpenProject", workingDir + Path.DirectorySeparatorChar + txtProjectName.Text + ".csproj");
this.Close();
return;
}
else if (string.IsNullOrEmpty(text))
{
return;
}

}


}

private string ExecuteProcess(System.Diagnostics.Process proc, string program, string arguments, out string error, string workingdir = "")
{
proc.StartInfo.UseShellExecute = false;

proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;

proc.StartInfo.FileName = program;
proc.StartInfo.Arguments = arguments;
if (!string.IsNullOrEmpty(workingdir))
{
proc.StartInfo.WorkingDirectory = workingdir;
}
proc.Start();
WaitForExit(proc);

error = proc.StandardError.ReadToEnd();
return proc.StandardOutput.ReadToEnd();
}

private string GetSelectedDependencies()
{
string dependenciesString = "";
foreach(var item in Dependencies.Where(d=> d.IsSelected))
{
dependenciesString += " --" + item.ShortName;
}
return dependenciesString;
}

private void WaitForExit(System.Diagnostics.Process process)
{
while(!process.HasExited)
{
System.Threading.Thread.Sleep(100);
}
}
private string GetSettingsPath()
{
var settingName = "VisualStudioProjectsLocation";
SettingsManager sm = new ShellSettingsManager(ServiceProvider.GlobalProvider);
SettingsStore ss = sm.GetReadOnlySettingsStore(SettingsScope.UserSettings);
string setting = "";
try
{
setting = ss.GetString("", settingName);
}
catch(Exception ex)
{
string collection = "";
foreach (string s in ss.GetPropertyNames(""))
{
collection += s + "\r\n";
}
MessageBox.Show($"Cannot find {settingName} in {collection}");
}
return Environment.ExpandEnvironmentVariables(setting);
}
private async Task<List<Dependency>> GetDependenciesAsync()
{
var url = "https://start.steeltoe.io/api/templates/dependencies";
try
{

var request = WebRequest.Create(url);
var response = request.GetResponse();
using (Stream dataStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(dataStream);
string responseBody = reader.ReadToEnd();
return JsonConvert.DeserializeObject<List<Dependency>>(responseBody);
}
}

catch (HttpRequestException e)
{
Console.WriteLine("\nException Caught!");
Console.WriteLine("Message :{0} ", e.Message);
}
return null;
}

}
public class Dependency
{
public string Name { get; set; }
public string ShortName { get; set; }
public string Description { get; set; }
public bool IsSelected { get; set; } = false;
}
}
Binary file added SteeltoeVsix/NewSteeltoeProject/Key.snk
Binary file not shown.
Loading

0 comments on commit cc30740

Please sign in to comment.