ryanackley avatar ryanackley committed 873836e

Added config file in the root directory style configuration

Comments (0)

Files changed (7)

ConfigFileBasedProject.js

+var isWin = navigator.platform.indexOf('Win') == 0;
+
+var ConfigFileBasedProject = function(config, root, url){
+	var routesStr = config.toFile;
+	var toFileRoutes = [];
+	for (var i = 0; i < routesStr.length; i++){
+		toFileRoutes.push({from: new RegExp(routesStr[i].from), to: routesStr[i].to});
+	}
+	this.toFileRoutes = toFileRoutes;
+	routesStr = config.fromFile;
+	var fromFileRoutes = [];
+	if (routesStr){
+		for (var i = 0; i < routesStr.length; i++){
+			fromFileRoutes.push({from: new RegExp(routesStr[i].from), to: routesStr[i].to});
+		}
+		this.fromFileRoutes = fromFileRoutes;
+	}
+	this.projectUrls = {};
+	this.rootPath = root.fullPath;
+	this.url = url;
+	if (isWin){
+		this.replaceSlashes = function(str){
+			return str.replace('/', '\\');
+		}
+	}
+	else{
+		this.replaceSlashes = function(str){return str;};
+	}
+}
+
+ConfigFileBasedProject.prototype = {
+	_doRegexConversion : function(str, root, from, to){
+		var match = str.match(from);
+		if (match){
+			var partialPath = match[0].replace(from, to);
+			var path = root + (partialPath.charAt(0) == '/' ? '' : '/') + partialPath;
+			return path;
+		}
+		return null;
+	},
+	filePathForUrl : function(url){
+		var routes = this.toFileRoutes;
+		for (var i = 0; i < routes.length; i++){
+			var path = this._doRegexConversion(url, this.rootPath, routes[i].from, routes[i].to);
+			if (path){
+				this.projectUrls[this.replaceSlashes(path)] = url;
+				return path;
+			}
+		}
+		return null;
+	},
+	
+	urlsForFilePath : function(path){
+		var routes = this.fromFileRoutes;
+		for (var i = 0; i < routes.length; i++){
+			var url = this._doRegexConversion(path, this.url, routes[i].from, routes[i].to);
+			if (url){
+				return [url];
+			}
+		}
+		var url = this.projectUrls[path];
+		if (url){
+			return [url];
+		}
+	 	return [];
+	},
+	resetUrls : function(){
+		this.projectUrls = {};
+	}
+}

ConfigFileBasedProjectLoader.js

+ProjectTypes.push(
+	{
+		name: 'Configuration File',
+		key: 'config.file',
+		locationType : 'local',
+		createProject : function(root, url, callback){
+			var handleError = function(e){
+				callback(null, e);
+			};
+			Gito.FileUtils.readFile(root, 'tincr.json', 'Text', function(json){
+				var config;
+				try{
+					config = JSON.parse(json);
+				}
+				catch(e){
+					handleError('Failed to parse JSON: ' + e.message);
+					return;
+				}
+				var project = new ConfigFileBasedProject(config, root, url);
+				callback(project, null);
+			}, function(){handleError('Unable to open ' + root.fullPath + '/tincr.json');});
+			
+		} 
+    }
+);

ProjectManager.js

 				projectType.createProject(dir,url,function(project, error){
 					self.fsRoot = fs.root;
 					project.matchedResourceMap = {};
+					if (!project.compare){
+						project.compare = function(a,b){
+							return a === b;
+						}
+					}
 					projectsByTab[tabId] = project;
 					sendResponse({path:path, error:error});
 				});
 			var path = currentProject.matchedResourceMap[url];
 			if (path){
 				Gito.FileUtils.readFile(this.fsRoot, path, 'Text', function(localContent){
-					if (content != localContent){
+					if (!currentProject.compare(content, localContent)){
 					
 						var ran = Math.round(Math.random()*10000000);
 						var handleError = function(){
 						}
 						
 						var handleContent = function(newContent){
-							if (localContent != newContent){
+							if (!currentProject.compare(newContent,localContent)){
 								handleError();
 							}else{
 								sendResponse({success: true, msg: 'Content for url ' + url + ' was reloaded and it matches local file ' + path, content: newContent });

RubyOnRailsProject.js

-var slash = navigator.platform.indexOf('Win') == 0 ? '\\' : '/';
-
-var RubyOnRails31Project = function(root, routes){
-	this.routes = routes;
-	this.rootPath = root.fullPath;
-	//used to strip off .coffee, .less, .scss, etc. off the end of a file name
-	this.stripRegex = /(\.js|\.css)\.[a-zA-Z]+$/
-	this.projectUrls = {};
-}
-
-RubyOnRails31Project.prototype = {
-	filePathForUrl : function(url){
-		var routes = this.routes;
-		for (var i = 0; i < routes.length; i++){
-			var match = url.match(routes[i].from);
-			if (match){
-				var partialPath = match[0].replace(routes[i].from, routes[i].to);
-				var path = this.rootPath + (partialPath.charAt(0) == '/' ? '' : '/') + partialPath;
-				this.projectUrls[path] = url;
-			}
-		}
-		return null;
-	},
-	
-	urlsForFilePath : function(path){
-		var strippedPath = path.replace(this.stripRegex, '$1');
-		var url = this.projectUrls[strippedPath];
-		if (url){
-			return [url];
-		}
-	 	return [];
-	},
-	resetUrls : function(){
-		this.projectUrls = {};
-	}
-}
 
 ProjectTypes.push(
     {
 		key: 'ror3.1',
 		locationType : 'local',
 		createProject : function(root, url, callback){
-			routes = [
-				{from: /\/assets\/(.+\.js)/,
-				 to: slash + ['app','assets','javascripts'].join(slash) + slash + '$1'},
-				{from: /\/assets\/(.+\.css)/,
-				 to: slash + ['app','assets','stylesheets'].join(slash) + slash + '$1'}
-			];
-			var project = new RubyOnRails31Project(root, routes);
+			config = {
+					toFile : [
+						{from: '/assets/(.+\\.js)',
+						 to: '/app/assets/javascripts/$1'},
+						{from: '/assets/(.+\\.css)',
+						 to: '/app/assets/stylesheets/$1'}
+					],
+					fromFile : [
+						{from: '(\\\\|/)app\\1assets\\1(?:javascripts|stylesheets)\\1(.+\\.js|\\.css)(\\.[a-zA-Z]+)?$',
+						 to: '/assets/$2?body=1'}
+					]
+			};
+			
+			var project = new ConfigFileBasedProject(config, root, url);
+			var trailer = /\s*;\s*$/;
+			project.compare = function(remote,local){
+				if (remote != local){
+					// sprockets puts a semicolon and whitespace trailer on the end of js files. This tries to create 
+					// a comparison that ignores the trailer.
+					var idx = remote.indexOf(local);
+					if (idx == 0){
+						return remote.substring(local.length).search(trailer) == 0;
+					}
+				}
+				else{
+					return true;
+				}
+				return false;
+			}
 			callback(project);
 		} 
     }
     		var watchersByTab={};
     	</script>
     	<script src="file_utils.js"></script>
+    	<script src="ConfigFileBasedProject.js"></script>
 		<script src="RubyOnRailsProject.js"></script>
 		<script src="ChromeExtensionProject.js"></script>
 		<script src="AtlassianPluginProject.js"></script>
 		<script src="zepto1.0rc1.js"></script>
 		<script src="RecentUpdateHandler.js"></script>
 		<script src="FileWatcher.js"></script>
-
+		<script src="ConfigFileBasedProjectLoader.js"></script>
 		<script src="ProjectManager.js"></script>
     	<embed type="application/x-npapifileioforchrome" id="pluginId" style="position:absolute; top:0px;left:-10000px;width:5px;height:5px;">
 
 			}
 		}
 		if (type && path && projectState.path != path){
-			backgroundMsgSupport.loadProject(type, path, inspectedLocation.href, function(){
+			backgroundMsgSupport.loadProject(type, path, inspectedLocation.origin, function(){
 				loadProject(type, path);
 			});
 		}
 			});
 			$('#native-browse').on('click', function(e){
 				var index = typeSelect.selectedIndex;
-				backgroundMsgSupport.launchFileSelect(index, window.devtoolsWindow.inspectedLocation.href, function(result){
+				backgroundMsgSupport.launchFileSelect(index, window.devtoolsWindow.inspectedLocation.origin, function(result){
 					if (result.path && result.path.length){
 						$('#local-file-path').text(result.path);
 						if (result.error){
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.