Tanoda
AutoCopywriteHeader.cs
Go to the documentation of this file.
1/******************************************************************************
2 * Copyright (C) Ultraleap, Inc. 2011-2020. *
3 * *
4 * Use subject to the terms of the Apache License 2.0 available at *
5 * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *
6 * between Ultraleap and you, your company or other organization. *
7 ******************************************************************************/
8
9using System;
10using System.IO;
11using System.Text;
12using System.Text.RegularExpressions;
13using System.Collections.Generic;
14using UnityEngine;
15using UnityEditor;
16
17public static class AutoCopywriteHeader {
18
19 private static Regex beginPattern = new Regex(@"^\/\*");
20 private static Regex endPattern = new Regex(@"\*\/" );
21
22
29 private static string[] apacheCopywriteNotice = {"/******************************************************************************",
30 " * Copyright (C) Ultraleap, Inc. 2011-2020. *",
31 " * *",
32 " * Use subject to the terms of the Apache License 2.0 available at *",
33 " * http://www.apache.org/licenses/LICENSE-2.0, or another agreement *",
34 " * between Ultraleap and you, your company or other organization. *",
35 " ******************************************************************************/"};
36
37 private static string[] searchFolders = { "LeapMotion" };
38
39 [MenuItem("Assets/Update Copywrite Headers")]
40 public static void PopulateAutoHeaders() {
41 List<string> files = new List<string>();
42 foreach (var folder in searchFolders) {
43 try {
44 files.AddRange(Directory.GetFiles(Path.Combine("Assets", "Plugins", folder), "*.cs", SearchOption.AllDirectories));
45 } catch (Exception e) {
46 Debug.LogException(e);
47 }
48 }
49
50 StringBuilder builder = new StringBuilder();
51
52 try {
53 for (int i = 0; i < files.Count; i++) {
54 string filename = files[i];
55
56 if (EditorUtility.DisplayCancelableProgressBar("Updating copywrite notices",
57 "Updating " + Path.GetDirectoryName(filename) + "...",
58 i / (files.Count - 1.0f))) {
59 return;
60 }
61
62 if (tryBuildFile(filename, builder)) {
63 File.WriteAllText(filename, builder.ToString());
64 } else {
65 Debug.LogWarning("Could not add header to " + filename);
66 }
67
68 builder.Length = 0;
69 }
70 } finally {
71 EditorUtility.ClearProgressBar();
72 AssetDatabase.Refresh();
73 }
74 }
75
76 private static bool tryBuildFile(string filename, StringBuilder builder) {
77 using (var reader = File.OpenText(filename)) {
78 string line;
79 do {
80 line = reader.ReadLine();
81
82 //Empty Cs file
83 if (line == null) {
84 return false;
85 }
86 } while (line.Trim().Length == 0);
87
88 //If we find a comment block already, skip past it, we are going to overwrite it!
89 if (beginPattern.IsMatch(line)) {
90 do {
91 line = reader.ReadLine();
92
93 //Unclosed closed comment block
94 if (line == null) {
95 return false;
96 }
97 } while (!endPattern.IsMatch(line));
98 line = reader.ReadLine();
99
100 //After we skip past the comment block, consume one extra empty line
101 if (line.Trim().Length == 0) {
102 line = reader.ReadLine();
103
104 //A file with just a single comment block
105 if (line == null) {
106 return false;
107 }
108 }
109 }
110
111 //Append the comment block first
112 //As of June 2020, UnityModules is fully licensed under Apache V2
113 foreach (var noticeLine in apacheCopywriteNotice) {
114 builder.AppendLine(noticeLine);
115 }
116 // // if (filename.Contains("North Star")) {
117 // // foreach (var noticeLine in apacheCopywriteNotice) {
118 // // builder.AppendLine(noticeLine);
119 // // }
120 // // } else {
121 // // foreach (var noticeLine in leapSdkCopywriteNotice) {
122 // // builder.AppendLine(noticeLine);
123 // // }
124 // // }
125
126 //Then append a single empty line
127 builder.AppendLine();
128
129 //Then append the single valid line we are holding onto
130 builder.AppendLine(line);
131
132 //Finally append the rest of the file
133 while (true) {
134 line = reader.ReadLine();
135
136 if (line == null) {
137 return true;
138 }
139
140 builder.AppendLine(line);
141 }
142 }
143 }
144}
145
146#region archive
147
148// Irrelevant as of June 2020, as UnityModules is now fully licensed under Apache V2. The original Leap Motion Developer SDK license header is below.
149 // private static string[] leapSdkCopywriteNotice = {"/******************************************************************************",
150 // " * Copyright (C) Ultraleap, Inc. 2011-2020. *",
151 // " * Ultraleap proprietary and confidential. *",
152 // " * *",
153 // " * Use subject to the terms of the Leap Motion SDK Agreement available at *",
154 // " * https://developer.leapmotion.com/sdk_agreement, or another agreement *",
155 // " * between Ultraleap and you, your company or other organization. *",
156 // " ******************************************************************************/"};
157
158#endregion
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19