Skip to content

Commit

Permalink
Add support for TypeScript
Browse files Browse the repository at this point in the history
Merge branch 'support-ts' into 'develop'

See merge request persper/code-analytics!120
  • Loading branch information
hezyin committed Jun 11, 2019
2 parents 908a6fc + 262dbd9 commit 87816bc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 13 deletions.
2 changes: 1 addition & 1 deletion persper/analytics/graph_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from persper.analytics.call_commit_graph import CallCommitGraph

JS_FILENAME_REGEXES = [
r'.+\.(js|vue)$',
r'.+\.(js|vue|ts|tsx)$',
r'^(?!dist/).+',
r'^(?!test(s)?/).+',
r'^(?!spec/).+',
Expand Down
52 changes: 43 additions & 9 deletions persper/analytics/multi_analyzer.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os
import json
import pickle
import logging
from git import Repo
from enum import Enum
from Naked.toolshed.shell import muterun_rb
from persper.util.path import root_path
from persper.analytics.c import CGraphServer
Expand All @@ -12,6 +14,23 @@
from persper.analytics.analyzer2 import Analyzer, AnalyzerObserver, emptyAnalyzerObserver


_logger = logging.getLogger(__name__)


class Linguist(Enum):
"""
Encodes the relationship between a language and its name in linguist's output
https://github.com/github/linguist/blob/master/lib/linguist/languages.yml
"""
C = "C"
CPP = "C++"
GO = "Go"
JAVA = "Java"
JAVASCRIPT = "JavaScript"
TYPESCRIPT = "TypeScript"
VUE = "Vue"


class MultiAnalyzer:

LANGUAGE_THRESHOLD = 0.3
Expand All @@ -35,21 +54,36 @@ def _set_linguist(self):
for k in lang_dict.keys():
lang_dict[k] = lang_dict[k] * 1.0 / total_lines

js = Linguist.JAVASCRIPT.value
ts = Linguist.TYPESCRIPT.value
vue = Linguist.VUE.value

_logger.info(lang_dict)
if vue in lang_dict:
_logger.info("Merging Vue to Javascript...")
if js not in lang_dict:
lang_dict[js] = 0
lang_dict[js] += lang_dict[vue]
del lang_dict[vue]
_logger.info(lang_dict)

if ts in lang_dict:
_logger.info("Merging TypeScript to JavaScript...")
if js not in lang_dict:
lang_dict[js] = 0
lang_dict[js] += lang_dict[ts]
del lang_dict[ts]
_logger.info(lang_dict)

# intersect with what languages we support
for lang, value in lang_dict.items():
if lang in self._supported_analyzers().keys():
self._linguist[lang] = value

print(self._linguist)
if "Vue" in self._linguist:
print("Merging Vue to Javascript...")
if "JavaScript" not in self._linguist:
self._linguist["JavaScript"] = 0
self._linguist["JavaScript"] += self._linguist["Vue"]
del self._linguist["Vue"]
print(self._linguist)
_logger.info(self._linguist)

else:
print('Analyzing Language Error from Linguist')
_logger.info('Analyzing Language Error from Linguist')

def _set_analyzers(self):
for language, value in self._linguist.items():
Expand Down
21 changes: 18 additions & 3 deletions test/test_analytics/test_multi_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,28 @@


@pytest.fixture(scope='module')
def repo_path():
def vue_repo_path():
repo_path = os.path.join(root_path, "repos/vue-realworld-example-app")
repo_url = 'https://github.com/gothinkster/vue-realworld-example-app'
if not os.path.exists(repo_path):
Repo.clone_from(repo_url, repo_path)
return repo_path


def test_set_linguist(repo_path):
maz = MultiAnalyzer(repo_path)
@pytest.fixture(scope='module')
def ts_repo_path():
repo_path = os.path.join(root_path, "repos/TypeScriptSamples")
repo_url = 'https://github.com/microsoft/TypeScriptSamples'
if not os.path.exists(repo_path):
Repo.clone_from(repo_url, repo_path)
return repo_path


def test_set_linguist_vue(vue_repo_path):
# _set_linguist is called during the initialization of MultiAnalyzer
maz = MultiAnalyzer(vue_repo_path)


def test_set_linguist_ts(ts_repo_path):
# _set_linguist is called during the initialization of MultiAnalyzer
maz = MultiAnalyzer(ts_repo_path)

0 comments on commit 87816bc

Please sign in to comment.