Start to implement WikiController.

Changed controllers from servlet to filter by mapping flexibility.
This commit is contained in:
takezoe
2013-05-02 02:01:51 +09:00
parent f1957e5c1f
commit 36ce53477f
14 changed files with 253 additions and 207 deletions

View File

@@ -4,8 +4,9 @@ import javax.servlet._
class ScalatraBootstrap extends LifeCycle { class ScalatraBootstrap extends LifeCycle {
override def init(context: ServletContext) { override def init(context: ServletContext) {
context.mount(new CreateRepositoryServlet, "/new") context.mount(new CreateRepositoryController, "/new")
context.mount(new RepositoryViewerServlet, "/*") context.mount(new WikiController, "/*")
context.mount(new RepositoryViewerController, "/*")
context.addListener(new ServletContextListener(){ context.addListener(new ServletContextListener(){
def contextInitialized(e: ServletContextEvent): Unit = { def contextInitialized(e: ServletContextEvent): Unit = {

View File

@@ -11,7 +11,7 @@ import jp.sf.amateras.scalatra.forms._
/** /**
* Creates new repository. * Creates new repository.
*/ */
class CreateRepositoryServlet extends ServletBase { class CreateRepositoryController extends ControllerBase {
case class RepositoryCreationForm(name: String, description: String) case class RepositoryCreationForm(name: String, description: String)

View File

@@ -64,7 +64,7 @@ case class ContentInfo(viewType: String, content: Option[String])
/** /**
* The repository viewer. * The repository viewer.
*/ */
class RepositoryViewerServlet extends ServletBase { class RepositoryViewerController extends ControllerBase {
/** /**
* Displays user information. * Displays user information.

View File

@@ -9,7 +9,7 @@ import jp.sf.amateras.scalatra.forms._
/** /**
* Provides generic features for ScalatraServlet implementations. * Provides generic features for ScalatraServlet implementations.
*/ */
abstract class ServletBase extends ScalatraServlet with ClientSideValidationFormSupport with JacksonJsonSupport { abstract class ControllerBase extends ScalatraFilter with ClientSideValidationFormSupport with JacksonJsonSupport {
implicit val jsonFormats = DefaultFormats implicit val jsonFormats = DefaultFormats

View File

@@ -0,0 +1,9 @@
package app
class UsersController extends ControllerBase {
get("/"){
}
}

View File

@@ -1,9 +0,0 @@
package app
class UsersServlet extends ServletBase {
get("/"){
}
}

View File

@@ -0,0 +1,14 @@
package app
import util.{WikiUtil, JGitUtil}
class WikiController extends ControllerBase {
get("/:owner/:repository/wiki"){
val owner = params("owner")
val repository = params("repository")
html.wiki(WikiUtil.getPage(owner, repository, "Home"), JGitUtil.getRepositoryInfo(owner, repository, servletContext))
}
}

View File

@@ -1,77 +1,97 @@
package util package util
import java.io.File import java.io.File
import java.util.Date import java.util.Date
import org.eclipse.jgit.api.Git import org.eclipse.jgit.api.Git
import org.apache.commons.io.FileUtils import org.apache.commons.io.FileUtils
import org.eclipse.jgit.lib.RepositoryBuilder
object WikiUtil {
object WikiUtil {
/**
* The model for wiki page. /**
* * The model for wiki page.
* @param name the page name *
* @param name the page name
* @param content the page content * @param content the page content
*/ */
case class WikiPageInfo(name: String, content: String) case class WikiPageInfo(name: String, content: String)
/** /**
* The model for wiki page history. * The model for wiki page history.
* *
* @param name the page name * @param name the page name
* @param committer the committer the committer * @param committer the committer the committer
* @param message the commit message * @param message the commit message
* @param date the commit date * @param date the commit date
*/ */
case class WikiPageHistoryInfo(name: String, committer: String, message: String, date: Date) case class WikiPageHistoryInfo(name: String, committer: String, message: String, date: Date)
/** /**
* Returns the directory of the wiki repository. * Returns the directory of the wiki repository.
*/ */
def getWikiRepositoryDir(owner: String, repository: String): File = def getWikiRepositoryDir(owner: String, repository: String): File =
new File("%s/%s/%s-wiki.git".format(Directory.RepositoryHome, owner, repository)) new File("%s/%s/%s-wiki.git".format(Directory.RepositoryHome, owner, repository))
/** /**
* Returns the directory of the wiki working directory which is cloned from the wiki repository. * Returns the directory of the wiki working directory which is cloned from the wiki repository.
*/ */
def getWikiWorkDir(owner: String, repository: String): File = def getWikiWorkDir(owner: String, repository: String): File =
new File("%s/tmp/%s/%s-wiki".format(Directory.RepositoryHome, owner, repository)) new File("%s/tmp/%s/%s-wiki".format(Directory.RepositoryHome, owner, repository))
// TODO synchronized?
/** def createWikiRepository(owner: String, repository: String): Unit = {
val dir = getWikiRepositoryDir(owner, repository)
if(!dir.exists){
val repo = new RepositoryBuilder().setGitDir(dir).setBare.build
repo.create
}
}
/**
* Returns the wiki page. * Returns the wiki page.
*/ */
def getPage(owner: String, repository: String, pageName: String): Option[WikiPageInfo] = { def getPage(owner: String, repository: String, pageName: String): Option[WikiPageInfo] = {
val git = Git.open(getWikiRepositoryDir(owner, repository)) createWikiRepository(owner, repository)
JGitUtil.getFileList(git, "master", ".").find(_.name == pageName).map { file => val git = Git.open(getWikiRepositoryDir(owner, repository))
WikiPageInfo(file.name, new String(git.getRepository.open(file.id).getBytes, "UTF-8")) try {
} JGitUtil.getFileList(git, "master", ".").find(_.name == pageName).map { file =>
} WikiPageInfo(file.name, new String(git.getRepository.open(file.id).getBytes, "UTF-8"))
}
// TODO } catch {
//def getPageHistory(owner: String, repository: String, pageName: String): List[WikiPageHistoryInfo] // TODO no commit, but it should not judge by exception.
case e: NullPointerException => None
// TODO synchronized }
/** }
* Save the wiki page.
*/ // TODO
def savePage(owner: String, repository: String, pageName: String, content: String, committer: String, message: String): Unit = { // def getPageList(owner: String, repository: String): List[WikiPageHistoryInfo]
val workDir = getWikiWorkDir(owner, repository)
// TODO
// clone //def getPageHistory(owner: String, repository: String, pageName: String): List[WikiPageHistoryInfo]
if(!workDir.exists){
Git.cloneRepository.setURI(getWikiRepositoryDir(owner, repository).toURI.toString).setDirectory(workDir).call // TODO synchronized
} /**
* Save the wiki page.
// write as file */
val file = new File(workDir, pageName + ".md") def savePage(owner: String, repository: String, pageName: String, content: String, committer: String, message: String): Unit = {
FileUtils.writeStringToFile(file, content, "UTF-8") createWikiRepository(owner, repository)
// commit and push val workDir = getWikiWorkDir(owner, repository)
val cloned = Git.open(workDir)
cloned.add.addFilepattern(file.getName).call // clone
cloned.commit.setAuthor(committer, committer + "@devnull").setMessage(message).call if(!workDir.exists){
cloned.push.call Git.cloneRepository.setURI(getWikiRepositoryDir(owner, repository).toURI.toString).setDirectory(workDir).call
} }
// write as file
val file = new File(workDir, pageName + ".md")
FileUtils.writeStringToFile(file, content, "UTF-8")
// commit and push
val cloned = Git.open(workDir)
cloned.add.addFilepattern(file.getName).call
cloned.commit.setAuthor(committer, committer + "@devnull").setMessage(message).call
cloned.push.call
}
} }

View File

@@ -2,7 +2,7 @@
@import context._ @import context._
@import view.helpers @import view.helpers
@main(repository.owner+"/"+repository.name) { @main(repository.owner+"/"+repository.name) {
@header(branch, repository) @header("code", repository)
@navtab(branch, repository, "files") @navtab(branch, repository, "files")
<div class="head"> <div class="head">
<a href="@path/@repository.owner/@repository.name/tree/@branch">@repository.name</a> / <a href="@path/@repository.owner/@repository.name/tree/@branch">@repository.name</a> /

View File

@@ -1,115 +1,115 @@
@(branch: String, commit: app.CommitInfo, repository: app.RepositoryInfo, diffs: Seq[app.DiffInfo])(implicit context: app.Context) @(branch: String, commit: app.CommitInfo, repository: app.RepositoryInfo, diffs: Seq[app.DiffInfo])(implicit context: app.Context)
@import context._ @import context._
@import view.helpers @import view.helpers
@import org.eclipse.jgit.diff.DiffEntry.ChangeType @import org.eclipse.jgit.diff.DiffEntry.ChangeType
@main(helpers.cut(commit.message, 20)){ @main(helpers.cut(commit.message, 20)){
@header(branch, repository) @header("code", repository)
@navtab(branch, repository, "commits") @navtab(branch, repository, "commits")
<table class="table table-bordered"> <table class="table table-bordered">
<tr> <tr>
<th> <th>
<div>@helpers.format(commit.message)</div> <div>@helpers.format(commit.message)</div>
<div class="small" style="font-weight: normal;"><span class="description">@branch</span></div> <div class="small" style="font-weight: normal;"><span class="description">@branch</span></div>
</th> </th>
</tr> </tr>
<tr> <tr>
<td> <td>
<a href="@path/@commit.committer">@commit.committer</a> <span class="description">@helpers.datetime(commit.time)</span> <a href="@path/@commit.committer">@commit.committer</a> <span class="description">@helpers.datetime(commit.time)</span>
<div class="pull-right align-right"> <div class="pull-right align-right">
<span class="description">commit</span> @commit.id <span class="description">commit</span> @commit.id
</div> </div>
</td> </td>
</tr> </tr>
</table> </table>
@diffs.zipWithIndex.map { case (diff, i) => @diffs.zipWithIndex.map { case (diff, i) =>
<table class="table table-bordered"> <table class="table table-bordered">
<tr> <tr>
<th style="font-weight: normal;"> <th style="font-weight: normal;">
@if(diff.changeType == ChangeType.COPY || diff.changeType == ChangeType.RENAME){ @if(diff.changeType == ChangeType.COPY || diff.changeType == ChangeType.RENAME){
@diff.oldPath -> @diff.newPath @diff.oldPath -> @diff.newPath
} }
@if(diff.changeType == ChangeType.ADD || diff.changeType == ChangeType.DELETE || diff.changeType == ChangeType.MODIFY){ @if(diff.changeType == ChangeType.ADD || diff.changeType == ChangeType.DELETE || diff.changeType == ChangeType.MODIFY){
@diff.newPath @diff.newPath
} }
<div class="pull-right align-right"> <div class="pull-right align-right">
<a href="@path/@repository.owner/@repository.name/blob/@commit.id/@diff.newPath" class="btn btn-small">View file @@ @commit.id.substring(0, 10)</a> <a href="@path/@repository.owner/@repository.name/blob/@commit.id/@diff.newPath" class="btn btn-small">View file @@ @commit.id.substring(0, 10)</a>
</div> </div>
</th> </th>
</tr> </tr>
<tr> <tr>
<td> <td>
@if(diff.newContent != None || diff.oldContent != None){ @if(diff.newContent != None || diff.oldContent != None){
<div id="diff-@i"></div> <div id="diff-@i"></div>
<textarea id="newText-@i" style="display: none;">@diff.newContent.getOrElse("")</textarea> <textarea id="newText-@i" style="display: none;">@diff.newContent.getOrElse("")</textarea>
<textarea id="oldText-@i" style="display: none;">@diff.oldContent.getOrElse("")</textarea> <textarea id="oldText-@i" style="display: none;">@diff.oldContent.getOrElse("")</textarea>
} else { } else {
Too big file not shown Too big file not shown
} }
</td> </td>
</tr> </tr>
</table> </table>
} }
} }
<script type="text/javascript" src="@path/assets/jsdifflib/difflib.js"></script> <script type="text/javascript" src="@path/assets/jsdifflib/difflib.js"></script>
<script type="text/javascript" src="@path/assets/jsdifflib/diffview.js"></script> <script type="text/javascript" src="@path/assets/jsdifflib/diffview.js"></script>
<link href="@path/assets/jsdifflib/diffview.css" type="text/css" rel="stylesheet" /> <link href="@path/assets/jsdifflib/diffview.css" type="text/css" rel="stylesheet" />
<style type="text/css"> <style type="text/css">
table.inlinediff { table.inlinediff {
width: 100%; width: 100%;
} }
table.inlinediff thead { table.inlinediff thead {
display: none; display: none;
} }
td.insert, td.equal, td.delete { td.insert, td.equal, td.delete {
width: 100%; width: 100%;
} }
</style> </style>
<script> <script>
function diffUsingJS(oldTextId, newTextId, outputId) { function diffUsingJS(oldTextId, newTextId, outputId) {
// get the baseText and newText values from the two textboxes, and split them into lines // get the baseText and newText values from the two textboxes, and split them into lines
var oldText = document.getElementById(oldTextId).value; var oldText = document.getElementById(oldTextId).value;
if(oldText == ''){ if(oldText == ''){
var oldLines = []; var oldLines = [];
} else { } else {
var oldLines = difflib.stringAsLines(oldText); var oldLines = difflib.stringAsLines(oldText);
} }
var newText = document.getElementById(newTextId).value var newText = document.getElementById(newTextId).value
if(newText == ''){ if(newText == ''){
var newLines = []; var newLines = [];
} else { } else {
var newLines = difflib.stringAsLines(newText); var newLines = difflib.stringAsLines(newText);
} }
// create a SequenceMatcher instance that diffs the two sets of lines // create a SequenceMatcher instance that diffs the two sets of lines
var sm = new difflib.SequenceMatcher(oldLines, newLines); var sm = new difflib.SequenceMatcher(oldLines, newLines);
// get the opcodes from the SequenceMatcher instance // get the opcodes from the SequenceMatcher instance
// opcodes is a list of 3-tuples describing what changes should be made to the base text // opcodes is a list of 3-tuples describing what changes should be made to the base text
// in order to yield the new text // in order to yield the new text
var opcodes = sm.get_opcodes(); var opcodes = sm.get_opcodes();
var diffoutputdiv = document.getElementById(outputId); var diffoutputdiv = document.getElementById(outputId);
while (diffoutputdiv.firstChild) diffoutputdiv.removeChild(diffoutputdiv.firstChild); while (diffoutputdiv.firstChild) diffoutputdiv.removeChild(diffoutputdiv.firstChild);
// build the diff view and add it to the current DOM // build the diff view and add it to the current DOM
diffoutputdiv.appendChild(diffview.buildView({ diffoutputdiv.appendChild(diffview.buildView({
baseTextLines: oldLines, baseTextLines: oldLines,
newTextLines: newLines, newTextLines: newLines,
opcodes: opcodes, opcodes: opcodes,
contextSize: 4, contextSize: 4,
viewType: 1 viewType: 1
})); }));
} }
$(function(){ $(function(){
@diffs.zipWithIndex.map { case (diff, i) => @diffs.zipWithIndex.map { case (diff, i) =>
@if(diff.newContent != None || diff.oldContent != None){ @if(diff.newContent != None || diff.oldContent != None){
diffUsingJS('oldText-@i', 'newText-@i', 'diff-@i'); diffUsingJS('oldText-@i', 'newText-@i', 'diff-@i');
} }
} }
}); });
</script> </script>

View File

@@ -2,7 +2,7 @@
@import context._ @import context._
@import view.helpers @import view.helpers
@main(repository.owner+"/"+repository.name) { @main(repository.owner+"/"+repository.name) {
@header(branch, repository) @header("code", repository)
@navtab(branch, repository, "commits") @navtab(branch, repository, "commits")
<div class="head"> <div class="head">
<a href="@path/@repository.owner/@repository.name/tree/@branch">@repository.name</a> / Commit History <a href="@path/@repository.owner/@repository.name/tree/@branch">@repository.name</a> / Commit History

View File

@@ -2,7 +2,7 @@
@import context._ @import context._
@import view.helpers @import view.helpers
@main(repository.owner+"/"+repository.name) { @main(repository.owner+"/"+repository.name) {
@header(branch, repository) @header("code", repository)
@navtab(branch, repository, "files") @navtab(branch, repository, "files")
<div class="head"> <div class="head">
<a href="@path/@repository.owner/@repository.name/tree/@branch">@repository.name</a> / <a href="@path/@repository.owner/@repository.name/tree/@branch">@repository.name</a> /

View File

@@ -1,4 +1,4 @@
@(branch: String, repository: app.RepositoryInfo)(implicit context: app.Context) @(active: String, repository: app.RepositoryInfo)(implicit context: app.Context)
@import context._ @import context._
<div class="head"> <div class="head">
<a href="@path/@repository.owner">@repository.owner</a> / <a href="@path/@repository.owner/@repository.name">@repository.name</a> <a href="@path/@repository.owner">@repository.owner</a> / <a href="@path/@repository.owner/@repository.name">@repository.name</a>
@@ -6,10 +6,10 @@
<div class="navbar"> <div class="navbar">
<div class="navbar-inner"> <div class="navbar-inner">
<ul class="nav"> <ul class="nav">
<li class="active"><a href="#">Code</a></li> <li@if(active=="code"){ class="active"}><a href="@path/@repository.owner/@repository.name">Code</a></li>
<li><a href="#">Issue</a></li> <li@if(active=="issue"){ class="active"}><a href="#">Issue</a></li>
<li><a href="#">Wiki</a></li> <li@if(active=="wiki"){ class="active"}><a href="@path/@repository.owner/@repository.name/wiki">Wiki</a></li>
<li><a href="#">Settings</a></li> <li@if(active=="settings"){ class="active"}><a href="#">Settings</a></li>
</ul> </ul>
</div> </div>
</div> </div>

View File

@@ -0,0 +1,11 @@
@(page: Option[util.WikiUtil.WikiPageInfo], repository: app.RepositoryInfo)(implicit context: app.Context)
@main("Wiki"){
@header("wiki", repository)
<ul class="nav nav-tabs">
<li><a href="">Home</a></li>
<li><a href="">Pages</a></li>
<li><a href="">Wiki History</a></li>
<li><a href="">Git Access</a></li>
</ul>
xxxx
}