Commits

ryanackley committed 4e8c00f

refactor to make it easier to create new project types

Comments (0)

Files changed (5)

AtlassianPluginProject.js

 AtlassianPluginProject = function(){
 	this.resourceMap = {};
-	this.matchedResourceMap = {};
 	this.matchedFileMap = {};
 	this.locationType = 'local';
 };
 			}); 
 		});
 	},
-	isProjectUrl: function (url){
+	filePathForUrl: function (url){
 		var resourceMap = this.resourceMap;
 		for (var end in resourceMap){
 			var idx = url.indexOf(end);
 			if (idx != -1 && idx == url.length - end.length){
 				var file = resourceMap[end];
-				this.matchedResourceMap[url] = file;
-				this.matchedFileMap[file.root.fullPath + file.path] = url;
-				return true;
+				var filePath = file.root.fullPath + file.path;
+				this.matchedFileMap[filePath] = url;
+				return filePath;
 			}
 		}
-		return false;
+		return null;
 	},
-	matchContents: function(url, content, callback){
-		var matchedResourceMap = this.matchedResourceMap;
-		var file = matchedResourceMap[url];
-		if (file){
-			Gito.FileUtils.readFile(file.root, file.path, 'Text', function(localContent){
-				if (content != localContent){
-					callback(false, 'Content for url ' + url + ' doesn\'t match local file ' + file.root.fullPath + file.path); 
-					delete matchedResourceMap[url];
-				}
-				else{
-					callback(true, 'Content for url ' + url + ' matches local file ' + file.root.fullPath + file.path);
-				}
-			});
-		}
-	},
-	commitResource : function(url, content, watcher, callback){
-		var localFile = this.matchedResourceMap[url];
-		if (localFile){
-			watcher.addChangedByMe(localFile.root.fullPath + localFile.path);
-			Gito.FileUtils.mkfile(localFile.root, localFile.path, content, callback);
-		}
-		else{
-			callback();
-		}
-	},
-	urlsForPath : function(path){
+	urlsForFilePath : function(path){
 		var single = this.matchedFileMap[path];
 		if (single){
 			return [single];
 		return [];
 	},
 	resetUrls : function(){
-		this.matchedResourceMap = {};
 		this.matchedFileMap = {};
 	}
 }

FileUrlProject.js

+FileUrlProject = function(){
+	this.projectUrls = {};
+};
+FileUrlProject.prototype = {
+	filePathForUrl : function(url){
+		if (url.indexOf('file://') == 0){
+			var path = url.substring(7);
+			this.projectUrls[path] = url;
+			return path;
+		}
+		return null;
+	},
+	urlsForFilePath : function(path){
+		// It's not necessary to cache the urls since it's just a matter of tacking on the 'file://' 
+		// in the front but I don't want to post an update 
+		// to the devtools panel unless I know it's an actual project url.
+		return this.projectUrls[path];
+	},
+	resetUrls : function(){
+		this.projectUrls = {};
+	}
+}
 		if (this.isMyChange(path)){
 			return;
 		}
-		var urls = this.project.urlsForPath(path)
+		var urls = this.project.urlsForFilePath(path)
 		if (urls && urls.length){
 			var port = chrome.tabs.connect(this.tabId, {name: 'fileChange'});
 			var ran = Math.round(Math.random()*10000000);

ProjectManager.js

 ProjectManager.prototype = {
 	launchFileSelect : function(tabId, typeIndex, sendResponse){
 		var projectsByTab = this.projectsByTab;
+		var self = this;
 		nativeFileSupport.launchFileSelect(function(path){
 			if (path && path.length){
 				var projectType = ProjectTypes[typeIndex];
 				window.requestFileSystem(window.PERMANENT, 5*1024*1024*1024, function(fs){
 					fs.root.getDirectory(path, {create:false}, function(dir){
 						projectType.createProject(dir,function(project, error){
-							currentProject = project;
-							projectsByTab[tabId] = currentProject;
+							self.fsRoot = fs.root;
+							project.matchedResourceMap = {};
+							projectsByTab[tabId] = project;
 							sendResponse({path:path, error:error});
 						});
 					});
 		if (currentProject){
 			for (var i = 0; i < urls.length; i++){
 				if (urls[i].type == 'script' || urls[i].type == 'stylesheet'){
-					retVal.push(currentProject.isProjectUrl(urls[i].url));
+					var url = urls[i].url;
+				    var filePath = currentProject.filePathForUrl(url);
+				    if (filePath){
+				    	currentProject.matchedResourceMap[url] = filePath;
+				    }
+				    retVal.push(filePath != null);
 				}
 				else{
 					retVal.push(false);
 	checkResourceContent : function(tabId, url, content, sendResponse){
 		var currentProject = this.projectsByTab[tabId];
 		if (currentProject){
-			currentProject.matchContents(url, content, function(success, msg){
-				sendResponse({success:success, msg:msg});
-			});
+			var path = currentProject.matchedResourceMap[url];
+			if (path){
+				Gito.FileUtils.readFile(this.fsRoot, path, 'Text', function(localContent){
+					if (content != localContent){
+						sendResponse({success: false, msg: 'Content for url ' + url + ' doesn\'t match local file ' + path}); 
+						delete currentProject.matchedResourceMap[url];
+					}
+					else{
+						sendResponse({success: true, msg: 'Content for url ' + url + ' matches local file ' + path });
+					}
+				});
+			}
 		}
 	},
 	
 	updateResource: function(tabId, url, content, sendResponse){
 		var currentProject = this.projectsByTab[tabId];
-		var currentWatcher = this.watchersByTab[tabId];
-		currentProject.commitResource(url, content, currentWatcher, sendResponse);
+		var watcher = this.watchersByTab[tabId];
+		//currentProject.commitResource(url, content, currentWatcher, sendResponse);
+		var localPath = currentProject.matchedResourceMap[url];
+		if (localPath){
+			watcher.addChangedByMe(localPath);
+			Gito.FileUtils.mkfile(this.fsRoot, localPath, content, sendResponse);
+		}
+		else{
+			sendResponse();
+		}
 	},
 	
 	resetProject : function(tabId, sendResponse){
 		var currentProject = this.projectsByTab[tabId];
 		currentProject.resetUrls();
+		currentProject.matchedResourceMap = {};
 		sendResponse();
 	},
 	watchDirectory : function(tabId, path){
 	<script src="RecentUpdateHandler.js"></script>
 	<script src="ResourceChecker.js"></script>
 	<script>
-		$.get('http://www.atlassian.com', function(data){
-			var x = 0;
-		});
 		$(document).ready(function(){
-			
+			console.log('ready');
 			var typeSelect = document.getElementById('project-type');
 			
 			backgroundMsgSupport.getProjectTypes(function(projectTypes){
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.