From 4470f1ab67597a0b907c537365b67ce4d7f704a0 Mon Sep 17 00:00:00 2001 From: st8ingenious Date: Fri, 12 Aug 2016 16:36:15 +0300 Subject: [PATCH] make builder recognize and fetch hpp files as headers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Now the builder “reads” the headers by their full file name (along with their extension, e.g. “SPI.h”). This is a better way to ensure that it will fetch the correct header. --- .../Controller/DefaultController.php | 30 ++++++++++++++----- .../BuilderBundle/Handler/DefaultHandler.php | 19 ++++++------ 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/Symfony/src/Codebender/BuilderBundle/Controller/DefaultController.php b/Symfony/src/Codebender/BuilderBundle/Controller/DefaultController.php index ef64167..386c1f1 100644 --- a/Symfony/src/Codebender/BuilderBundle/Controller/DefaultController.php +++ b/Symfony/src/Codebender/BuilderBundle/Controller/DefaultController.php @@ -130,7 +130,7 @@ protected function returnProvidedAndFetchedLibraries($projectFiles, $userLibrari { $apiHandler = $this->get('codebender_builder.handler'); - $detectedHeaders = $apiHandler->readLibraries($projectFiles); + $detectedHeaderstemp = $apiHandler->readLibraries($projectFiles); // declare arrays $notFoundHeaders = []; @@ -139,15 +139,15 @@ protected function returnProvidedAndFetchedLibraries($projectFiles, $userLibrari $providedLibraries = array_keys($userLibraries); $libraries = $userLibraries; - foreach ($detectedHeaders as $header) { + foreach ($detectedHeaderstemp as $header) { $existsInRequest = false; // TODO We can do this in a better way foreach ($userLibraries as $library) { foreach ($library as $libraryContent) { - if ($libraryContent["filename"] == $header.".h") { + if ($libraryContent["filename"] == $header) { $existsInRequest = true; - $foundHeaders[] = $header . ".h"; + $foundHeaders[] = $header; } } } @@ -155,24 +155,38 @@ protected function returnProvidedAndFetchedLibraries($projectFiles, $userLibrari if ($existsInRequest === true) { continue; } - $requestContent = ["type" => "fetch", "library" => $header]; + + //Fetch lib from library manager by name + $headername = pathinfo($header, PATHINFO_FILENAME); + + $requestContent = ["type" => "fetch", "library" => $headername]; $data = $this->getLibraryInfo(json_encode($requestContent)); if ($data['success'] === false) { - $notFoundHeaders[] = $header . ".h"; + $notFoundHeaders[] = $header; continue; } - $foundHeaders[] = $header . ".h"; + $foundHeaders[] = $header; $librariesFromLibman[] = $header; $filesToBeAdded = []; foreach ($data["files"] as $file) { - if (in_array(pathinfo($file['filename'], PATHINFO_EXTENSION), array('cpp', 'h', 'c', 'S', 'inc'))) + if (in_array(pathinfo($file['filename'], PATHINFO_EXTENSION), array('cpp', 'h', 'hpp', 'c', 'S', 'inc'))) $filesToBeAdded[] = $file; } $libraries[$header] = $filesToBeAdded; } + /* + * Get only the names of the header files. + * Until we make codebender handles headers along with their extensions (e.g. "Ethernet.h", not "Ethernet"), + * this must stay here for compatibility reasons. + */ + foreach ($detectedHeaderstemp as $headerfiles) { + $detectedHeaders[] = pathinfo($headerfiles, PATHINFO_FILENAME); + } + + // store info about libraries and headers in the `additionalCode` class property; $this->additionalCode = [ 'providedLibraries' => $providedLibraries, diff --git a/Symfony/src/Codebender/BuilderBundle/Handler/DefaultHandler.php b/Symfony/src/Codebender/BuilderBundle/Handler/DefaultHandler.php index f0b1971..ce16020 100644 --- a/Symfony/src/Codebender/BuilderBundle/Handler/DefaultHandler.php +++ b/Symfony/src/Codebender/BuilderBundle/Handler/DefaultHandler.php @@ -48,19 +48,20 @@ function detectHeadersInFile($code) { * Examples: * #include * # include "proto.h" - * + * #include "jsonlib.hpp" */ - $arrowsRegex = "/^\s*#\s*include\s*<\s*([a-zA-Z0-9_+]*)\.h\s*>/"; - $quotesRegex = "/^\s*#\s*include\s*\"\s*([a-zA-Z0-9_+]*)\.h\s*\"/"; + $arrowsRegex = "/^\s*#\s*include\s*<\s*([a-zA-Z0-9_+]*\.h)\s*([p]{2})*\s*>/"; + $quotesRegex = "/^\s*#\s*include\s*\"\s*([a-zA-Z0-9_+]*\.h)\s*([p]{2})*\s*\"/"; $headers = ["arrows" => [], "quotes" => []]; + $matches= [[]]; foreach (explode("\n", $code) as $line) { - if (preg_match($arrowsRegex, $line, $matches)) - $headers["arrows"][] = $matches[1]; - if (preg_match($quotesRegex, $line, $matches)) - $headers["quotes"][] = $matches[1]; - } + if (preg_match($arrowsRegex, $line, $matches)){ + $headers["arrows"][] = $matches[1].$matches[2];} + if (preg_match($quotesRegex, $line, $matches)){ + $headers["quotes"][] = $matches[1].$matches[2]; + }} $headers["arrows"] = array_unique($headers["arrows"]); $headers["quotes"] = array_unique($headers["quotes"]); @@ -88,7 +89,7 @@ function readLibraries($sketchFiles) { foreach ($headers["quotes"] as $key => $header) { foreach ($sketchFiles as $file) { - if ($file["filename"] == $header.".h") + if ($file["filename"] == $header) unset($headers["quotes"][$key]); } }