Skip to content

Commit

Permalink
Merge pull request #4 from simon-winter/feature/templates
Browse files Browse the repository at this point in the history
Feature/templates
  • Loading branch information
simon-winter authored Aug 4, 2023
2 parents 5405408 + 5a33834 commit f2572ed
Show file tree
Hide file tree
Showing 16 changed files with 813 additions and 389 deletions.
81 changes: 43 additions & 38 deletions Classes/ButtonCommands/CreateFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,50 +10,55 @@
using System.Windows.Forms;
using System.Windows.Input;

namespace UnityDotsAuthoringGenerator.Classes
{
public class CreateFile : ICommand
namespace UnityDotsAuthoringGenerator.Classes {
public class CreateFile : ICommand {
private string m_path;
private string m_content;
private string m_extension;

public CreateFile(string extension, string content)
{
private string m_path;
private string m_content;
private string m_extension;

public CreateFile(string path, string extension, string content) {
m_path = path;
m_content = content;
m_extension = extension;
}
m_content = content;
m_extension = extension;
}

public void Execute(object parameter) {
try {
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
var canceled = !TextInputDialog.Show("Enter name", "Enter the name for the desired file:", "",
delegate (string obj) { return obj != ""; }, out var name);
if (canceled) {
return;
}

if (!name.Contains(".")) {
name += m_extension;
}
var filePath = Path.Combine(m_path, name);

m_content = Regex.Replace(m_content,"TEMPLATENAME_", Path.GetFileNameWithoutExtension(name));
File.WriteAllText(filePath, m_content);
DteHelper.GetProject().ProjectItems.AddFromFile(filePath);
public void Execute(object parameter)
{
try {
Microsoft.VisualStudio.Shell.ThreadHelper.ThrowIfNotOnUIThread();
var canceled = !TextInputDialog.Show("Enter name", "Enter the name for the desired file:", "",
delegate(string obj) { return obj != ""; }, out var name);
if (canceled) {
return;
}
catch (Exception ex) {
Utils.ShowErrorBox(ex.Message);
if (!name.Contains(".")) {
name += m_extension;
}
}

public event EventHandler CanExecuteChanged {
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}
var path = Utils.AskUserForPath("Choose destination folder", DteHelper.GetSelectedPath(), "");
if (path == "") {
return;
}

var filePath = Path.Combine(path, name);

public bool CanExecute(object parameter) {
return true;
m_content = Regex.Replace(m_content, "TEMPLATENAME_", Path.GetFileNameWithoutExtension(name));
File.WriteAllText(filePath, m_content);
DteHelper.GetProject().ProjectItems.AddFromFile(filePath);
} catch (Exception ex) {
Utils.ShowErrorBox(ex.Message);
}
}

public event EventHandler CanExecuteChanged
{
add => CommandManager.RequerySuggested += value;
remove => CommandManager.RequerySuggested -= value;
}

public bool CanExecute(object parameter)
{
return true;
}
}
}
38 changes: 38 additions & 0 deletions Classes/Checkbox.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace UnityDotsAuthoringGenerator.Classes {

internal class Checkbox {
public string Name { get; set; }

public bool Checked
{
get {
if (SettingsManager.Instance.TryGet(Name) != "True") {
return false;
}
return true;
}
set {
SettingsManager.Instance.Set(Name, value.ToString());
}
}

public Checkbox(string name, bool defaultChecked)
{
Name = name;
var isChecked = SettingsManager.Instance.TryGet(name);

if (isChecked == "") {
SettingsManager.Instance.Set(Name, defaultChecked.ToString());
SettingsManager.Instance.SaveSettings();
} else {
Checked = bool.Parse(isChecked);
}
}
}
}
112 changes: 85 additions & 27 deletions Classes/DteHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,47 +8,105 @@
using System.Text;
using System.Threading.Tasks;
using UnityDotsAuthoringGenerator.Classes;
using System.Windows;

namespace UnityDotsAuthoringGenerator
{
internal class DteHelper
namespace UnityDotsAuthoringGenerator {
internal class DteHelper {
public static DTE2 GetDte()
{
public static DTE2 GetDte() {
return AsyncPackage.GetGlobalService(typeof(SDTE)) as DTE2;
return AsyncPackage.GetGlobalService(typeof(SDTE)) as DTE2;
}

public static string GetSelectedPath()
{
ThreadHelper.ThrowIfNotOnUIThread();
if (IsSolutionExplorerFocused()) {
return GetSelectedSolutionFileDirectory();
} else if (GetActiveDocument() != null) {
return GetActiveDocument().Path;
} else {
return "";
}
}

public static string GetSelectedFileDirectory() {
var filePath = GetSelectedFilePath();
return Utils.GetAsDirectory(filePath);
public static string GetSelectedFilePath()
{
ThreadHelper.ThrowIfNotOnUIThread();
if (IsSolutionExplorerFocused()) {
return GetSelectedSolutionFilePath();
} else {
return GetActiveDocument().FullName;
}
}

public static string GetSelectedSolutionFileDirectory()
{
var filePath = GetSelectedSolutionFilePath();
return Utils.GetAsDirectory(filePath);
}

public static string GetSelectedFilePath() {
ThreadHelper.ThrowIfNotOnUIThread();
public static string GetSelectedSolutionFilePath()
{
ThreadHelper.ThrowIfNotOnUIThread();

UIHierarchy uih = GetDte().ToolWindows.SolutionExplorer;
Array selectedItems = (Array)uih.SelectedItems;
if (null != selectedItems) {
foreach (UIHierarchyItem selItem in selectedItems) {
var prjItem = selItem.Object as ProjectItem;
string filePath = prjItem.Properties.Item("FullPath").Value.ToString();
return filePath;
}
UIHierarchy uih = GetDte().ToolWindows.SolutionExplorer;
Array selectedItems = (Array)uih.SelectedItems;
if (null != selectedItems) {
foreach (UIHierarchyItem selItem in selectedItems) {
var prjItem = selItem.Object as ProjectItem;
string filePath = prjItem.Properties.Item("FullPath").Value.ToString();
return filePath;
}
}
return string.Empty;
}

public static Document GetActiveDocument()
{
ThreadHelper.ThrowIfNotOnUIThread();
try {
return GetDte().ActiveDocument;
} catch (Exception ex) {

MessageBox.Show(string.Format("Failed getting active document: ", ex.Message),
"Error", MessageBoxButton.OK, MessageBoxImage.Error);
return null;
}
}

public static bool IsSolutionExplorerFocused()
{
ThreadHelper.ThrowIfNotOnUIThread();
try {
if (GetDte().ActiveWindow.ObjectKind.Equals(EnvDTE.Constants.vsWindowKindSolutionExplorer,
StringComparison.OrdinalIgnoreCase)) {
return true;
}
return string.Empty;
} catch (Exception ex) {

MessageBox.Show(string.Format("Failed checking if solution explorer is focused: ", ex.Message),
"Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
return false;
}

public static Project GetProject() {
ThreadHelper.ThrowIfNotOnUIThread();
public static Project GetProject()
{
ThreadHelper.ThrowIfNotOnUIThread();

UIHierarchy uih = GetDte().ToolWindows.SolutionExplorer;
Array selectedItems = (Array)uih.SelectedItems;
if (null != selectedItems) {
foreach (UIHierarchyItem selItem in selectedItems) {
var prjItem = selItem.Object as ProjectItem;
UIHierarchy uih = GetDte().ToolWindows.SolutionExplorer;
Array selectedItems = (Array)uih.SelectedItems;
if (null != selectedItems) {
foreach (UIHierarchyItem selItem in selectedItems) {
if (selItem.Object is ProjectItem prjItem) {
return prjItem.ContainingProject;
}
if (selItem.Object is Project prj) {
return prj;
}
}
return null;
}
return null;
}
}
}
Loading

0 comments on commit f2572ed

Please sign in to comment.