Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Phodal
Coca
提交
70071e91
C
Coca
项目概览
Phodal
/
Coca
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
C
Coca
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
70071e91
编写于
12月 25, 2019
作者:
P
Phodal Huang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: add basic tood
上级
ea34e11a
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
288 addition
and
0 deletion
+288
-0
cmd/todo.go
cmd/todo.go
+33
-0
core/domain/todo/astitodo/README.md
core/domain/todo/astitodo/README.md
+21
-0
core/domain/todo/astitodo/astitodo.go
core/domain/todo/astitodo/astitodo.go
+208
-0
core/domain/todo/todo_app.go
core/domain/todo/todo_app.go
+26
-0
未找到文件。
cmd/todo.go
0 → 100644
浏览文件 @
70071e91
package
cmd
import
(
"github.com/phodal/coca/core/domain/todo"
"github.com/spf13/cobra"
)
type
RootCmdConfig
struct
{
Path
string
}
var
(
rootCmdConfig
RootCmdConfig
)
var
todoCmd
=
&
cobra
.
Command
{
Use
:
"todo"
,
Short
:
"scan todo"
,
Long
:
``
,
Run
:
func
(
cmd
*
cobra
.
Command
,
args
[]
string
)
{
path
:=
cmd
.
Flag
(
"path"
)
.
Value
.
String
()
if
path
!=
""
{
app
:=
todo
.
NewTodoApp
()
app
.
AnalysisPath
(
path
)
}
},
}
func
init
()
{
rootCmd
.
AddCommand
(
todoCmd
)
todoCmd
.
PersistentFlags
()
.
StringVarP
(
&
rootCmdConfig
.
Path
,
"path"
,
"p"
,
"."
,
"path"
)
}
core/domain/todo/astitodo/README.md
0 → 100644
浏览文件 @
70071e91
MIT License
Copyright (c) 2016 Quentin Renard
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
\ No newline at end of file
core/domain/todo/astitodo/astitodo.go
0 → 100644
浏览文件 @
70071e91
package
astitodo
import
(
"encoding/csv"
"encoding/json"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
)
// Vars
var
(
regexpAssignee
=
regexp
.
MustCompile
(
"^
\\
([
\\
w
\\
._
\\
+
\\
-@]+
\\
)"
)
todoIdentifiers
=
[]
string
{
"TODO"
,
"FIXME"
}
)
// TODOs represents a set of todos
type
TODOs
[]
*
TODO
// TODO represents a todo
type
TODO
struct
{
Assignee
string
Filename
string
Line
int
Message
[]
string
}
// Extract walks through an input path and extracts TODOs from all files it encounters
func
Extract
(
path
string
,
excludedPaths
...
string
)
(
todos
TODOs
,
err
error
)
{
err
=
todos
.
extract
(
path
,
excludedPaths
...
)
return
}
func
(
todos
*
TODOs
)
extract
(
path
string
,
excludedPaths
...
string
)
error
{
return
filepath
.
Walk
(
path
,
func
(
path
string
,
info
os
.
FileInfo
,
err
error
)
error
{
// Process error
if
err
!=
nil
{
return
err
}
// Skip excluded paths
for
_
,
p
:=
range
excludedPaths
{
if
p
==
path
{
if
info
.
IsDir
()
{
return
filepath
.
SkipDir
}
return
nil
}
}
// Skip vendor and all directories beginning with a .
if
info
.
IsDir
()
&&
(
info
.
Name
()
==
"vendor"
||
(
len
(
info
.
Name
())
>
1
&&
info
.
Name
()[
0
]
==
'.'
))
{
return
filepath
.
SkipDir
}
// Only process go files
if
!
info
.
IsDir
()
&&
filepath
.
Ext
(
path
)
!=
".go"
{
return
nil
}
// Everything is fine here, extract if path is a file
if
!
info
.
IsDir
()
{
if
err
=
todos
.
extractFile
(
path
);
err
!=
nil
{
return
err
}
}
return
nil
})
}
func
(
todos
*
TODOs
)
extractFile
(
filename
string
)
(
err
error
)
{
// Parse file and create the AST
var
fset
=
token
.
NewFileSet
()
var
f
*
ast
.
File
if
f
,
err
=
parser
.
ParseFile
(
fset
,
filename
,
nil
,
parser
.
ParseComments
);
err
!=
nil
{
return
}
// Loop in comment groups
for
_
,
cg
:=
range
f
.
Comments
{
// Loop in comments
var
todo
*
TODO
var
TODOFound
bool
for
_
,
c
:=
range
cg
.
List
{
// Loop in lines
for
i
,
l
:=
range
strings
.
Split
(
c
.
Text
,
"
\n
"
)
{
// Init text
var
t
=
strings
.
TrimSpace
(
l
)
if
strings
.
HasPrefix
(
t
,
"//"
)
||
strings
.
HasPrefix
(
t
,
"/*"
)
||
strings
.
HasPrefix
(
t
,
"*/"
)
{
t
=
strings
.
TrimSpace
(
t
[
2
:
])
}
// To do found
if
length
,
isTodo
:=
isTodoIdentifier
(
t
);
isTodo
{
// Init to do
todo
=
&
TODO
{
Filename
:
filename
,
Line
:
fset
.
Position
(
c
.
Slash
)
.
Line
+
i
}
t
=
strings
.
TrimSpace
(
t
[
length
:
])
if
strings
.
HasPrefix
(
t
,
":"
)
{
t
=
strings
.
TrimLeft
(
t
,
":"
)
t
=
strings
.
TrimSpace
(
t
)
}
// Look for assignee
if
todo
.
Assignee
=
regexpAssignee
.
FindString
(
t
);
todo
.
Assignee
!=
""
{
t
=
strings
.
TrimSpace
(
t
[
len
(
todo
.
Assignee
)
:
])
if
strings
.
HasPrefix
(
t
,
":"
)
{
t
=
strings
.
TrimLeft
(
t
,
":"
)
t
=
strings
.
TrimSpace
(
t
)
}
todo
.
Assignee
=
todo
.
Assignee
[
1
:
len
(
todo
.
Assignee
)
-
1
]
}
// Append text
todo
.
Message
=
append
(
todo
.
Message
,
t
)
*
todos
=
append
(
*
todos
,
todo
)
TODOFound
=
true
}
else
if
TODOFound
&&
len
(
t
)
>
0
{
todo
.
Message
=
append
(
todo
.
Message
,
t
)
}
else
{
TODOFound
=
false
}
}
}
}
return
}
func
isTodoIdentifier
(
s
string
)
(
int
,
bool
)
{
for
_
,
indent
:=
range
todoIdentifiers
{
if
strings
.
HasPrefix
(
strings
.
ToUpper
(
s
),
indent
)
{
return
len
(
indent
),
true
}
}
return
0
,
false
}
// AssignedTo returns TODOs which are assigned to the specified assignee
func
(
todos
TODOs
)
AssignedTo
(
assignees
...
string
)
(
filteredTODOs
TODOs
)
{
for
_
,
t
:=
range
todos
{
for
_
,
assignee
:=
range
assignees
{
if
assignee
==
t
.
Assignee
{
filteredTODOs
=
append
(
filteredTODOs
,
t
)
}
}
}
return
}
// WriteText writes the TODOs as text to the specified writer
func
(
todos
TODOs
)
WriteText
(
w
io
.
Writer
)
(
err
error
)
{
for
_
,
t
:=
range
todos
{
if
t
.
Assignee
!=
""
{
if
_
,
err
=
io
.
WriteString
(
w
,
fmt
.
Sprintf
(
"Assignee: %s
\n
"
,
t
.
Assignee
));
err
!=
nil
{
return
}
}
if
_
,
err
=
io
.
WriteString
(
w
,
fmt
.
Sprintf
(
"Message: %s
\n
File:%s:%d
\n\n
"
,
strings
.
Join
(
t
.
Message
,
"
\n
"
),
t
.
Filename
,
t
.
Line
));
err
!=
nil
{
return
}
}
return
}
// WriteCSV writes the TODOs as CSV to the specified writer
// The columns are "Filename", "Line", "Assignee" and "Message" (which can contain newlines)
func
(
todos
TODOs
)
WriteCSV
(
w
io
.
Writer
)
(
err
error
)
{
var
c
=
csv
.
NewWriter
(
w
)
// Write the headings for the document
if
err
=
c
.
Write
([]
string
{
"Filename"
,
"Line"
,
"Assignee"
,
"Message"
});
err
!=
nil
{
return
}
for
_
,
t
:=
range
todos
{
err
=
c
.
Write
([]
string
{
t
.
Filename
,
strconv
.
Itoa
(
t
.
Line
),
t
.
Assignee
,
strings
.
Join
(
t
.
Message
,
"
\n
"
),
})
if
err
!=
nil
{
return
}
}
c
.
Flush
()
return
}
// WriteJSON writes the TODOs as JSON to the specified writer
func
(
todos
TODOs
)
WriteJSON
(
w
io
.
Writer
)
(
err
error
)
{
enc
:=
json
.
NewEncoder
(
w
)
err
=
enc
.
Encode
(
todos
)
return
}
core/domain/todo/todo_app.go
0 → 100644
浏览文件 @
70071e91
package
todo
import
(
"fmt"
"github.com/phodal/coca/core/domain/todo/astitodo"
"log"
)
type
TodoApp
struct
{
}
func
NewTodoApp
()
TodoApp
{
return
*&
TodoApp
{
}
}
func
(
a
TodoApp
)
AnalysisPath
(
path
string
)
{
todos
,
err
:=
astitodo
.
Extract
(
path
)
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
for
_
,
todo
:=
range
todos
{
fmt
.
Println
(
todo
.
Message
,
todo
.
Line
,
todo
.
Filename
)
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录