/Dev Tools/BlockGenerator/Pages/ObsidianEnumsPage.xaml.cs

https://github.com/SparkDevNetwork/Rock · C# · 177 lines · 141 code · 33 blank · 3 comment · 9 complexity · 9b9761cbb5db995974229114301b5494 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Runtime.CompilerServices;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using BlockGenerator.FileGenerators;
  11. using BlockGenerator.Utility;
  12. namespace BlockGenerator.Pages
  13. {
  14. /// <summary>
  15. /// Interaction logic for ObsidianEnumsPage.xaml
  16. /// </summary>
  17. public partial class ObsidianEnumsPage : Page
  18. {
  19. public ObsidianEnumsPage()
  20. {
  21. InitializeComponent();
  22. RockEnumsOutOfDateAlert.Visibility = SupportTools.IsSourceNewer( typeof( Rock.Model.Gender ).Assembly.Location, "Rock.Enums" )
  23. ? Visibility.Visible
  24. : Visibility.Collapsed;
  25. var types = GetEnumTypes();
  26. var typeItems = types.Select( t => new TypeItem( t ) )
  27. .ToList();
  28. foreach ( var item in typeItems )
  29. {
  30. var unsupported = !item.Type.FullName.StartsWith( "Rock.Enums." )
  31. && item.Type.GetCustomAttributes().FirstOrDefault( a => a.GetType().FullName == "Rock.Enums.EnumDomainAttribute" ) == null;
  32. if ( unsupported )
  33. {
  34. item.IsInvalid = true;
  35. item.IsExporting = false;
  36. item.InvalidReason = $"This enum is not in the correct namespace and cannot be exported.";
  37. }
  38. }
  39. typeItems = typeItems
  40. .OrderBy( t => !t.IsInvalid )
  41. .ThenBy( t => t.Name )
  42. .ToList();
  43. EnumsListBox.ItemsSource = typeItems;
  44. }
  45. private List<Type> GetEnumTypes()
  46. {
  47. return typeof( Rock.Enums.Reporting.FieldFilterSourceType ).Assembly
  48. .GetExportedTypes()
  49. .Where( t => t.IsEnum )
  50. .ToList();
  51. }
  52. private string GetPathForType( Type type )
  53. {
  54. if ( type.Namespace.StartsWith( "Rock.Model" ) )
  55. {
  56. var domainAttribute = type.GetCustomAttributes()
  57. .FirstOrDefault( a => a.GetType().FullName == "Rock.Enums.EnumDomainAttribute" );
  58. if ( domainAttribute == null )
  59. {
  60. throw new Exception( "Attempt to export an enum with an invalid namespace, this shouldn't happen." );
  61. }
  62. var domain = ( string ) domainAttribute.GetType().GetProperty( "Domain" ).GetValue( domainAttribute );
  63. return Path.Combine( "Rock.JavaScript.Obsidian", "Framework", "Enums", domain );
  64. }
  65. var components = type.Namespace.Replace( "Rock.Enums", string.Empty ).Trim( '.' ).Split( '.' );
  66. return Path.Combine( "Rock.JavaScript.Obsidian", "Framework", "Enums", string.Join( "\\", components ) );
  67. }
  68. private string GetFileNameForType( Type type )
  69. {
  70. return $"{type.Name.Split( '`' )[0].CamelCase()}.d.ts";
  71. }
  72. private IList<Type> GetSelectedTypes()
  73. {
  74. return EnumsListBox.ItemsSource
  75. .Cast<TypeItem>()
  76. .Where( t => t.IsExporting )
  77. .Select( t => t.Type )
  78. .ToList();
  79. }
  80. private void SelectAll_Click( object sender, RoutedEventArgs e )
  81. {
  82. EnumsListBox.ItemsSource
  83. .Cast<TypeItem>()
  84. .Where( i => !i.IsInvalid )
  85. .ToList()
  86. .ForEach( i => i.IsExporting = true );
  87. }
  88. private void SelectNone_Click( object sender, RoutedEventArgs e )
  89. {
  90. EnumsListBox.ItemsSource
  91. .Cast<TypeItem>()
  92. .Where( i => !i.IsInvalid )
  93. .ToList()
  94. .ForEach( i => i.IsExporting = false );
  95. }
  96. private async void Preview_Click( object sender, RoutedEventArgs e )
  97. {
  98. var files = new List<GeneratedFile>();
  99. var generator = new TypeScriptViewModelGenerator();
  100. foreach ( var type in GetSelectedTypes() )
  101. {
  102. var source = generator.GenerateViewModelForEnum( type );
  103. files.Add( new GeneratedFile( GetFileNameForType( type ), GetPathForType( type ), source ) );
  104. }
  105. await this.Navigation().PushPageAsync( new GeneratedFilePreviewPage( files ) );
  106. }
  107. private class TypeItem : IComparable, INotifyPropertyChanged
  108. {
  109. public event PropertyChangedEventHandler PropertyChanged;
  110. public Type Type { get; set; }
  111. public string Name { get; set; }
  112. public bool IsExporting
  113. {
  114. get => _isExporting;
  115. set
  116. {
  117. _isExporting = value;
  118. OnPropertyChanged();
  119. }
  120. }
  121. private bool _isExporting;
  122. public bool IsInvalid { get; set; }
  123. public string InvalidReason { get; set; }
  124. public TypeItem( Type type )
  125. {
  126. Type = type;
  127. Name = type.Name;
  128. IsExporting = true;
  129. if ( Name.StartsWith( "Rock.ViewModels." ) )
  130. {
  131. Name = Name.Substring( 15 );
  132. }
  133. }
  134. protected virtual void OnPropertyChanged( [CallerMemberName] string propertyName = null )
  135. {
  136. PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( propertyName ) );
  137. }
  138. public int CompareTo( object obj )
  139. {
  140. return Type.AssemblyQualifiedName.CompareTo( obj );
  141. }
  142. }
  143. }
  144. }