-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwizard.script
169 lines (132 loc) · 4.63 KB
/
wizard.script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
////////////////////////////////////////////////////////////////////////////////
//
// Code::Blocks new file wizard script
//
// Project: PL/D source file
// Author: Austin Hastings
//
// License: GPL v3.
//
////////////////////////////////////////////////////////////////////////////////
New_file_path <- _T( "newFile.d" );
Save_cwd <- _T( "" );
FILE_SEP_PATH <- wxFILE_SEP_PATH.GetChar( 0 );
DOT <- '.';
wxDOT <- _T( "." );
Include( _T( "default_content.script" ) );
function BeginWizard()
{
local intro_msg =
_T("Welcome to the new PL/D source file wizard.\n"
+ "This wizard will guide you to create a new D source file.\n\n"
+ "When you 're ready to proceed, please click \"Next\"..."
);
local active_file = GetEditorManager().GetActiveEditor().GetFilename();
local path = active_file.BeforeLast( FILE_SEP_PATH );
if( path.length() != 0 ) {
New_file_path = path
+ wxFILE_SEP_PATH
+ _T( "newFile.d" );
}
// Add welcome page.
Wizard.AddInfoPage( _T("DFileIntro"), intro_msg);
// Add file path page
Wizard.AddFilePathPage( false );
}
function OnEnter_FilePathPage( is_forward ) {
if( is_forward ) {
// Save the cwd, and cd to file-dir
Save_cwd = IO.GetCwd();
IO.SetCwd( New_file_path.BeforeLast( FILE_SEP_PATH ) );
Wizard.SetFilePathSelectionFilter( _T("D sources (*.d; *.di)|*.d;*.di") );
Wizard.SetTextControlValue( _T( "ID_TEXTCTRL1" ), New_file_path );
}
}
function OnLeave_FilePathPath( is_forward ) {
if( is_forward ) {
// Restore cwd
IO.SetCwd( Save_cwd );
}
}
function compare_wxstrings( left, right ) {
//Log( _T( "\nComparing strings |" )
// + left + _T( "| and |" ) + right +_T( "|" ) );
if( left != right ) { Log( _T( "\tdifferent" ) ); } else { Log( _T( "\t! different" ) ); }
if( left > right ) { Log( _T( "\tgreater" ) ); } else { Log( _T( "\t! greater" ) ); }
if( left < right ) { Log( _T( "\tlesser" ) ); } else { Log( _T( "\t! lesser" ) ); }
if( left == right ) { Log( _T( "\tsame" ) ); } else { Log( _T( "\t! same" ) ); }
}
// WTF? This is provided by wxWidgets, but stupid CB developers didn't bind it?
function starts_with( long_str, short_str ) {
local length = short_str.length();
local prefix = long_str.Mid( 0, length );
// This is incredibly bogus. But needful.
return prefix >= short_str && short_str >= prefix;
}
function get_relative_path( path ) {
local project = GetProjectManager().GetActiveProject();
local proj_dir = project.GetCommonTopLevelPath();
if( ! starts_with( path, proj_dir ) ) {
ShowWarning(
_T( "The file you have selected is not under the active project path. "
"You will have to adjust the \\file and module info by hand.\n" ) );
return path;
}
else {
local relative_path = path.Mid( proj_dir.length(), -1 );
return relative_path;
}
}
function module_name_from_relative_path( relative_path ) {
// relative_path = "foo/bar/baz.d"
local module_name = relative_path.BeforeLast( DOT );
// module_name = "foo/bar/baz"
module_name.Replace( wxFILE_SEP_PATH, wxDOT, true );
// module_name = "foo.bar.baz"
if( starts_with( module_name, _T( "source." ) )
|| starts_with( module_name, _T( "sources." ) )
|| starts_with( module_name, _T( "srcs." ) )
|| starts_with( module_name, _T( "src." ) )
|| starts_with( module_name, _T( "t." ) )
|| starts_with( module_name, _T( "tests." ) ) ) {
// NB: I didn't include "test." on purpose - it could go either way.
module_name = module_name.AfterFirst( DOT );
}
return module_name;
}
function CreateFiles() {
local ed = GetEditorManager();
if (IsNull(ed)) {
ShowError(_T("The wizard could not locate the editor manager."));
}
local fname = Wizard.GetFileName();
local ed_new = ed.New(fname);
if (IsNull(ed_new)) {
ShowError(_T("The wizard could not create a new file.\n"
+ "Maybe the target folder is write-protected?"));
}
else { // succeeded -> add file to project if needed
if( ed_new.GetText().IsEmpty() ) {
local shortname = ed_new.GetShortName();
local basename = shortname.BeforeLast( '.' );
local relative_path = get_relative_path( fname );
local module_name = module_name_from_relative_path( relative_path );
local default_text = _T( "// $Id: $\n"
+ "/**\n"
+ " ***** ERROR: d_source/default_content.d missing or incorrect.\n"
+ "*/" );
if( Default_content ) {
default_text = Default_content;
}
default_text.Replace( _T("@BASENAME@"), basename );
default_text.Replace( _T("@MODULE_NAME@"), module_name );
default_text.Replace( _T("@RELATIVE_PATH@"), relative_path);
default_text.Replace( _T("@SHORTNAME@"), shortname );
ed_new.SetText( default_text );
}
if (Wizard.GetFileAddToProject()) {
AddFileToTargets(Wizard, fname);
}
}
return fname;
}